lnk2001 error - unresolved externals (C++)

233 Views Asked by At

This is going to be tricky because I can't post any real code, but here goes:

I have inheritances in the form A->B->C, where C is the class I'm writing that inherits from B, B inherits from A. A and B were both written by other people and I can't change them (they appear to work standalone). I also have a TestC class, which #includes C.h because it creates an object with which to test; C class is empty, TestC only creates a C object. When I build my TestC project in Visual Studio 2013, I get lnk2001 errors; these go away if I comment out the #include C.h. The C project compiles without issue.

A.h
#pragma once
class A {
    public:
        A() {};
        virtual ~A() {};
        virtual int SomeGoodFunction() {return 0;};
        virtual int BadFunction() {return 0;}; //lnk 2001: unresolved external
    private:
        virtual int SomeOtherFunction() {};
        virtual int SomeOtherBadFunction() {}; //lnk 2001: unresolved external
};

B.h
#pragma once
namespace Model {
#include "A.h"
class B : public A {
    public:
        B() {}; //lnk 2019: unresolved external referenced in C()
        virtual ~B() {}; //lnk 2019: unresolved external referenced in ~C()
    private:
        void Bad() {}; //lnk 2001: unresolved external
        virtual ThisFunctionWorks() {};
        void SoDoesThis() {};
};}

C.h
#pragma once
#include "B.h"
class C : public B {
    friend class TestC;
    public:
        C() {};
        ~C() {};
        int Initialize() {};
};

TestC.h
#pragma once
#include "C.h"
namespace Model {
class TestC {
    int Test();
};
}

TestC.cpp
#pragma once
#include "TestC.h"
namespace Model {
int TestC::Test() {
    C test = C(); //if I comment out, builds just fine
    return 0;
}
}

(A.h & B.h have corresponding .cpp's where things are defined, but the contents aren't relevant, I don't think; every function that it's complaining about has been defined in the .cpp (I can click 'view definition' and VS will jump to it. Please pardon any syntax errors; I wrote this as psuedo code and since I'm getting linker errors after compiling syntax, I'm pretty sure they don't exist in the original code.)

The lnk2001 errors are saying they can't find the definition of virtual functions in A.h and can't find the constructor, deconstructor, and one function (non-virtual) of B.h. I can open A & B's .h/.cpp files and see the functions it's complaining about being defined. TestC's include directories has these included (and I can open them by clicking on the #includes, so it's working). Project dependencies are set for build order. From a programming standpoint, C doesn't really NEED to inherit from B, but the higher ups made the decision and my peon self can't change it. I'm guessing it has to do with some nuance of inheritance and/or virtual functions that I'm not aware of.

What am I missing? I feel like I've gone through most Stack Overflow articles...

EDIT: Each of these are in their own projects, with everything except TestC being a .lib, TestC is a .exe. When I use this basic code all in the same project, it compiles with no errors, so I'm thinking it's an issue between the C & TestC project.

0

There are 0 best solutions below