[C++-sig] boost.python class constructor error

Jim Bosch talljimbo at gmail.com
Fri Mar 12 04:38:52 CET 2010


On Fri, 2010-03-12 at 10:36 +0800, hitesh dhiman wrote:
> This error occurs when i declare the class constructor in the c++
> header file and the definition in the .cpp file. Bjam throws up a
> LNK2019 error, unresolved symbol.
> But if i declare the class constructor in the header file itself, the
> code compiles.
> Here's the code:
> 
> 
> Test1.h
> #include <windows.h>
> //using namespace std;
> 
> 
> class World
> {
> public:
> World();
> ~World();
>     void set(std::string msg) { this->msg = msg; }
>     std::string greet() { return msg; }
>     std::string msg;
> };
> 
> 
> Test1.cpp
> #include "Test1.h"
> 
> 
> 
> 
> World::World() {};
> World::~World() {};
> int addition(int a, int b)
> {
> int z = a + b;
> return z;
> }
> 
> 
> Wrapper file:
> #include <boost/python.hpp>
> #include "Test1.h"
> 
> 
> 
> 
> BOOST_PYTHON_MODULE(hello)
> {
>     using namespace boost::python;
> class_<World>("World")
> 
>         .def("greet", &World::greet)
>         .def("set", &World::set)
>     ;
> }
> 
> 
> Is it a compiler bug? I want to define the constructors in the cpp
> file, and not the header file. 
> -- 

I've never had any problems doing exactly that, and I can't say more
about your particular situation without seeing your build system setup
(which probably actually wouldn't help me, since I don't know much about
bjam or Windows).

Are you certain your source file is getting compiled into (or otherwise
linked) to the module's shared library?

Did you compile the source file with the same compiler flags as the
wrapper file?  They probably don't have to be identical, but some may be
important.

My guess that it's not just the constructor; you aren't linking against
anything in your non-wrapper cpp file.


Jim





More information about the Cplusplus-sig mailing list