0

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;
}
8
  • Nowhere did you mention what modules you actually compiled for ConsoleApp2, or what modules your project uses to build ConsoleApp2. Telling us what headers are where does not resolve linker errors -- all it does is make the compiler happy, but the compiler is not the linker. More than likely, you're failing to compile a dependent module, and/or your project settings for the ConsoleApp2 does not list the module that the linker is looking for. Commented Apr 22, 2014 at 17:52
  • I used VS2012 to compile, but can you explain what you mean by modules? I thought by including the path of ConsolApp1 to be included in ConsolApp2 I should be able to include the header files of ConsolApp1 without linking errors. So is there a property setting that I forgot to modify in ConsolApp2? Commented Apr 22, 2014 at 17:55
  • Your answer indicates you're not understanding the compile/link process. When you create an application, the project must list all of the .cpp and other module files necessary to be built (plus any third-party libraries), thus the linker will find the relevant object code for each function that is called. The linker doesn't magically search your directory for previous compilations from another project. Each project is separate and apart. Commented Apr 22, 2014 at 17:56
  • Well so far in all other projects that I have done where they needed header files from different projects, I haven't had any problems linking them by just including the path to where the header files are located Commented Apr 22, 2014 at 18:00
  • When you compile a module (.cpp file for example), you create an object file (usually with a .obj extension). When it's time to link, the linker is taking the list of .obj files it is given, and searches for the functions you have called. If it can't find the function, then it gives the error. Again, the linker (at least the Microsoft linker) is not intelligent enough to figure out what module you compiled on some other project, and therefore use that module. Commented Apr 22, 2014 at 18:01

1 Answer 1

0

but ConsolApp2 does not compile and throws three link errors.

Wrong, ConsolApp2.cpp does compile. The issue is not compilation, it is linking.

In your project, you should be compiling

  • FooDerived.cpp
  • FooImplement.cpp
  • ImplementMain.cpp.

If you're not, then add the missing module(s) to your project.

In this case, it seems to be FooDerived.cpp that is not being compiled or not being part of the Visual Studio project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.