I have created a base and derived class in one project, say called ConsolApp1, where the base class has a couple virtual methods and a virtual destructor. The methods are all set to be pure virtual methods which are then defined in the derived class and overridden using the override keyword. Also, both the derived and base classes are wrapped in a namespace. When I create a new project, say called ConsolApp2 and located in the same solution as ConsolApp1, which implements objects of the derived class Link Errors appear for any method or destructor that was stated as being virtual. To allow ConsolApp2 to include the derived class and the namespace that it lies in, I had to add the path to the location of the header file. I am pretty sure I did that properly as the header file shows up when I try to include it. Thanks in advance for your help.
Here is some pseudo code for the problem I am having. I was able to compile ConsolApp1 with no errors, but ConsolApp2 does not build and throws three link errors, two for the virtual methods and one for the virtual destructor. Compiling was done using VS2012. Errors are:
error LNK2001: unresolved external symbol "public: virtual int const __thiscall FooSpace::FooDerived::GetSomething(void)const " (?GetSomething@FooDerived@FooSpace@@UBE?BHXZ)
error LNK2001: unresolved external symbol "public: virtual void __thiscall FooSpace::FooDerived::SetSomething(int)" (?SetSomething@FooDerived@FooSpace@@UAEXH@Z)
error LNK2019: unresolved external symbol "public: virtual __thiscall FooSpace::FooDerived::~FooDerived(void)" (??1FooDerived@FooSpace@@UAE@XZ) referenced in function "public: virtual void * __thiscall FooSpace::FooDerived::`scalar deleting destructor'(unsigned int)" (??_GFooDerived@FooSpace@@UAEPAXI@Z)
ConsolApp1:
FooBase.h
namespace FooSpace
{
class FooBase
{
public:
FooBase(){}
virtual ~FooBase() {}
virtual const int GetSomething() const = 0;
virtual void SetSomething(int f) = 0;
};
}
FooDerived.h
#include "FooBase.h"
namespace FooSpace
{
class FooDerived : FooBase
{
public:
FooDerived() : FooBase(){}
~FooDerived() override;
const int GetSomething() const override;
void SetSomething(int f) override;
};
}
FooDerived.cpp
#include "FooDerived.h"
FooSpace::FooDerived::~FooDerived()
{
//destruct an object of FooDerived
}
const int FooSpace::FooDerived::GetSomething() const
{
int ret = 0;
return ret;
}
void FooSpace::FooDerived::SetSomething(int f)
{
// Set some private variable equal to f
}
FooMain.cpp -> included to make sure there isn't an error for a missing main()
#include "FooDerived.h"
using namespace FooSpace;
int main()
{
FooDerived derivedObject;
return 0;
}
ConsolApp2:
FooImplement.h
#include <FooDerived.h>
#include <vector>
using namespace std;
using namespace FooSpace;
class FooImpliment
{
private:
vector<FooDerived> fooVector;
public:
FooImpliment(void);
~FooImpliment(void);
void SetFooVector(vector<FooDerived> newVector);
};
FooImplement.cpp
#include "FooImpliment.h"
FooImpliment::FooImpliment(void)
{
}
FooImpliment::~FooImpliment(void)
{
}
void FooImpliment::SetFooVector(vector<FooDerived> newVector)
{
fooVector = newVector;
}
ImplementMain.cpp -> included to get rid of error for missing main()
int main()
{
return 0;
}