1

I'm trying to build a DLL which will manage various configuration options for my project as a Singleton.

I followed the method proposed here in the chosen answer, as shown below:

IBuildConfiguration.h

#if defined(BUILD_CONFIGURATION_LIBRARY_EXPORT)
#   define BUILD_CONFIGURATION_API   __declspec(dllexport)
#else
#   define BUILD_CONFIGURATION_API   __declspec(dllimport)
#endif  // BUILD_CONFIGURATION_LIBRARY_EXPORT

class IBuildConfiguration
{
    public:
        virtual int foo(void) = 0; 
};

BUILD_CONFIGURATION_API IBuildConfiguration& Instance(void);

BuildConfiguration.h

class BuildConfiguration : public IBuildConfiguration
{
    public:

        BuildConfiguration();
        ~BuildConfiguration();


        virtual int foo(void);
};

Edit: Forgot to include implementation of Instance()

BuildConfiguration.cpp

int BuildConfiguration::foo(void)
{
    return 1; //just a silly example
}

IBuildConfiguration& Instance(void)
{
    static BuildConfiguration instance;
    return instance;
}

Now, in Visual C++ 6, I added to my project a dependency on this new DLL and I included the IBuildConfiguration header in my source as such:

SystemCtrl.cpp

#include "../../BuildConfiguration/IBuildConfiguration.h"

IBuildConfiguration buildConfig = Instance();

My DLL builds successfully, however the project that uses it does not.

Unfortunately, this results in the following error:

int __thiscall IBuildConfiguration::foo(void)' : pure virtual function was not defined

copying the DLL and LIB files into the project doesn't seem to solve the issue.

Community
  • 1
  • 1
audiFanatic
  • 2,296
  • 8
  • 40
  • 56
  • I think the Instance method is trying to create an instance of your base class. which cannot be done for the reason reported. – Rob Mar 04 '16 at 20:36
  • @Rob See my comment below; my bad, I didn't provide the implementation of `Instance()` – audiFanatic Mar 04 '16 at 21:03

2 Answers2

2

make sure your Instance method looks like this

 IBuildConfiguration& Instance()
 {
    // Creates instance of derived class all is good.
    static BuildConfiguration inst;
    return inst;
 }

and not:

 IBuildConfiguration& Instance()
 {
    // Tries to create instance of base class.. Not possible as it
    // has pure virtual functions.
    static IBuildConfiguration inst;
    return inst;
 }

Dark Falcon is right, you need to store the return from the Instance() method in a reference to the base class or it will try to create a new instance of the base class by calling the base class copy constructor same problem :).

Rob
  • 2,618
  • 2
  • 22
  • 29
2

If IBuildConfiguration did not have a pure virtual function, this would still not work as you're expecting:

IBuildConfiguration buildConfig = Instance();

This basically says "copy-construct an instance of IBuildConfiguration from the return value of Instance()". The derived class info is lost. This is slicing. You need your variable to be a reference:

IBuildConfiguration& buildConfig = Instance();
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • I was just about to update my answer with this :) congrats. that should do it. – Rob Mar 04 '16 at 21:18
  • It does solve the problem, however, now I'm getting another error: `unresolved external symbol "__declspec(dllimport) class IBuildConfiguration & __cdecl Instance(void)" ` – audiFanatic Mar 04 '16 at 21:23
  • Then you didn't properly link to the `.lib` file for your DLL. I don't remember exactly how, but it is in linker options. (It has been at least 8 years since I used VC++ 6.) – Dark Falcon Mar 04 '16 at 21:24
  • @DarkFalcon perfect, I added `/libpath:"..\BuildConfiguration\Debug\\" ` to the project options – audiFanatic Mar 04 '16 at 21:39