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

Paul Melis paul at science.uva.nl
Mon Apr 21 13:30:19 CEST 2008


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



More information about the Python-list mailing list