views:

212

answers:

2

In a plugin-based C++ project, I have a TmpClass that is used to exchange data between the main application and the plugins. Therefore the respective TmpClass.h is included in the abstract plugin interface class that is included by the main application project, and implemented by each plugin.

As the plugins work on STL vectors of TmpClass instances, there needs to be a default constructor and destructor for the TmpClass. I had declared these in TmpClass.h:

class TmpClass {
  TmpClass();
  ~TmpClass();
}

and implemented them in TmpClass.cpp.

TmpClass::~TmpClass() {}
TmpClass::TmpClass() {}

However, when compiling plugins this leads to the linker complaining about two unresolved externals - the default constructor and destructor of TmpClass as required by the std::vector<TmpClass> template instantiation - even though all other functions I declare in TmpClass.h and implement in TmpClass.cpp work. As soon as I remove the (empty) default constructor and destructor from the .cpp file and inline them into the class declaration in the .h file, the plugins compile and work.

Why is it that the default constructor and destructor have to be inline for this code to compile? Why does it even maatter? (I'm using MSVC++8).

+2  A: 

The behavior you describe simply means that you forgot to include the TmpClass.cpp file into the project.

The compiler cannot and will not magically know where the non-inline class methods are defined. It is your responsibility to compile all .cpp files and link them together. In MSVC it is normally done by adding all .cpp files to the project.

AndreyT
Thanks for the reply, but the .cpp file is added to the project and compiles. As I said, I have other functions declared in the header and implemented in the .cpp file, and these functions work, and that includes a non-default constructor. It is just the default constructor and destructor that show up as unresolved when they are not inlined.
Anamon
@Anamon - you might have included the .cpp file in the main application project. But thats not enough to compile one of your plugin project. your plugin projects never know where the definition of your Ctor and Dtor is.
SysAdmin
This is completely embarrassing. TUrns out that I indeed dragged the wrong file onto my plugin projects and they couldn't find the .cpp. I should'nt underestimate my ability to overlook the most obvious things. Thanks for your help!
Anamon
A: 

I'm guessing you have one "main application" project and one or more "plugin" projects and it looks like you haven't included TmpClass.cpp in the plugins project. I'm also guessing "all other function you declare in the .h and implement in .cpp" are only used by your main project and not by your plugins.

As others have said, you can include TmpClass.cpp across your plugin projects. The other option is to create a dll "sdk" project and link both the main and plugins project against it.

If you still think this and AndreyT's answers are wrong, you should provide some more information about your projects structure.

sbk