By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,214 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,214 IT Pros & Developers. It's quick & easy.

SWIG (Python) - "no constructor defined" for concrete class

P: n/a
Yet another SWIG question (YASQ!).

I'm having a problem with using an abstract base class. When
generating the Python bindings, SWIG thinks that all the concrete
classes that derive from this abstract class are abstract too and
won't create the correct constructor.

Abstract class:

[source lang="cpp"]class CORE_API Shape
{
public:
virtual ~Shape()
{
nshapes--;
};
double x, y;
virtual void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
static int nshapes;
protected:
Shape() {
nshapes++;
}

};
[/source]

Derived classes:

[source lang="cpp"]class CORE_API Circle : public Shape
{
private:
double radius;
public:
Circle(double r): Shape(), radius(r)
{
};
virtual double area(void);
virtual double perimeter(void);
};

class CORE_API Square : public Shape
{
private:
double width;
public:
Square(double r): Shape(), width(r)
{
};
virtual double area(void);
virtual double perimeter(void);
};
[/source]

SWIG file:

[source lang="cpp"]class Shape
{
virtual void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
};

class Circle: public Shape
{
Circle(double r);
virtual double area(void);
virtual double perimeter(void);
};
class Square: public Shape
{
Square(double r);
virtual double area(void);
virtual double perimeter(void);
};
[/source]

C++ COde:

[source lang="cpp"] Circle c(1.02);
std::cout << "(c++)\t\tCircle\t" << c.area() << std::endl;
Square s(9.20);
std::cout << "(c++)\t\tSquare\t" << s.area() << std::endl;

[/source]

For some reason SWIG thinks that Circle and Square are abstract. Any
ideas why? I'm rather confused by this.

Thanks as always
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Stodge wrote:
Yet another SWIG question (YASQ!).

I'm having a problem with using an abstract base class. When
generating the Python bindings, SWIG thinks that all the concrete
classes that derive from this abstract class are abstract too and
won't create the correct constructor.

Abstract class:

[source lang="cpp"]class CORE_API Shape
{
public:
virtual ~Shape()
{
nshapes--;
};
double x, y;
virtual void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
static int nshapes;
protected:
Shape() {
nshapes++;
}

};
[/source]

Derived classes:

[source lang="cpp"]class CORE_API Circle : public Shape
{
private:
double radius;
public:
Circle(double r): Shape(), radius(r)
{
};
virtual double area(void);
virtual double perimeter(void);
};

class CORE_API Square : public Shape
{
private:
double width;
public:
Square(double r): Shape(), width(r)
{
};
virtual double area(void);
virtual double perimeter(void);
};
[/source]

SWIG file:

[source lang="cpp"]class Shape
{
virtual void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
};

class Circle: public Shape
{
Circle(double r);
virtual double area(void);
virtual double perimeter(void);
};
class Square: public Shape
{
Square(double r);
virtual double area(void);
virtual double perimeter(void);
};
[/source]

C++ COde:

[source lang="cpp"] Circle c(1.02);
std::cout << "(c++)\t\tCircle\t" << c.area() << std::endl;
Square s(9.20);
std::cout << "(c++)\t\tSquare\t" << s.area() << std::endl;

[/source]

For some reason SWIG thinks that Circle and Square are abstract. Any
ideas why? I'm rather confused by this.
See section 6.6.2 of the SWIG documentation (SWIG and C++ -Default
constructors, copy constructors and implicit destructors).

Your abstract base class defines its default constructor in the
protected section. From the docs:

"Default constructors and implicit destructors are not created if any
base class defines a non-public default constructor or destructor."

Paul
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.