Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:09:13+00:00 2026-06-14T06:09:13+00:00

I have C++ class and need to use it in python code. For that,

  • 0

I have C++ class and need to use it in python code. For that, using SWIG to generate a wrapper class. As per the documentation configured the example.i

/* File: example.i */
%module example

%{
#define SWIG_FILE_WITH_INIT
#include "item.h"
#include "GradedComplex.h"
#include "GradedDouble.h"
%}

%include "item.h"
%include "GradedComplex.h"
%include "GradedDouble.h"

And tried to build it using following command

c:>swig -c++ -python example.i

c:>python setup.py build_ext --inplace

The item.h is :

#ifndef __ITEM_H__
#define __ITEM_H__

#include <complex>
#include <functional>
#include <string>

template<typename T>
class Item
{
  std::string name_;
  T val_;

public:
  Item(std::string name, T val) : name_(name), val_(val) {}
  Item(Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  T operator()() const { return val_; }
  double norm() const { return sqrt(val_ * val_); }
  Item<T> &operator+=(Item<T> &rhs)
  {
    val_ += rhs();
    return *this;
  }
  Item<T> &operator-=(Item<T> &rhs)
  {
    val_ -= rhs();
    return *this;
  }
  Item<T> &operator*=(Item<T> &rhs)
  {
    val_ *= rhs();
    return *this;
  }
};

template<>
class Item<std::complex<double> >
{
  std::string name_;
  std::complex<double> val_;

public:
  Item(std::string name, std::complex<double> val) : name_(name), val_(val) {}
  Item(Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  std::complex<double> operator()() const { return val_; }
  double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag()); }
};

template<typename T>
struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool>
{
  inline bool operator()(Item<T> lhs, Item<T> rhs)
  {
    return lhs.norm() < rhs.norm();
  }
};

#endif

But still I am getting the following error

example_wrap.cxx(3275) : error C2512: 'Item<std::complex<double>>' : no appropriate default constructor available

c:\documents and settings\swig\Item.h(38) : warning C45
21: 'Item<T>' : multiple copy constructors specified
        with
        [
            T=double
        ]
        example_wrap.cxx(3425) : see reference to class template instantiation '
Item<T>' being compiled
        with
        [
            T=double
        ]
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2

Please give me some suggestion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:09:14+00:00Added an answer on June 14, 2026 at 6:09 am

    Use this option for building extension…
    swig -nodefaultctor example.i

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below. I need to use a class id instead of
I have a class which is going to need to use the strategy design
I have to use class Matrix4x4 from some 3rd party library, and I need
I have a simple class for use on JNI, which i need to export
I have class grades that I need to check if a specific grade is
I have a class that I need to perform some actions on but I
I have a class that I need to be able to serialize to a
I have written code within Python that doesn't release memory the way it should.
I find myself writing this class often in my python code when I need
I have class Meat extends Food { $var = Food::foodFunction… } I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.