2

I'm trying to create an object of class A. Compilation works fine, but the linker complains about a LNK2019 unresolved external symbol D::D referenced in function A::A.

A.cpp

#include "../A.hpp"

using namespace <name>;

A::A(D* low, D* mid, D* high, M* m)
{
    std::vector<B*>* lTC = split(low);
    std::vector<B*>* mTC = split(mid);
    std::vector<B*>* hTC = split(high);

    D* lDC = new D(lTC);
    D* mDC = new D(mTC);
    D* hDC = new D(hTC);

    mr = m;

    procRDC = new std::vector<D*>();
    procRDC->push_back(lDC); 
    procRDC->push_back(mDC);
    procRDC->push_back(hDC);
}

std::vector<B*>* A::split(D* d)
{
    std::vector<B*>* tc = new std::vector<B*>();
    std::vector<B*>* ob = d->getB();

    for ( std::vector<B*>::iterator it = ob->begin(); it != ob->end(); ++it )
    {
        B* b= *it;

        int a1 = b->getA;
        int a2 = b->getA;
        int a3 = b->getA;

        B* b1 = new B(a1, a2, a3);
        B* b2 = new B(a3, a2, a1);

        tc ->push_back(b1);
        tc ->push_back(b2);
    }

    return tc;
}

A.hpp

#ifndef A_HPP
#define A_HPP

#include "../dec.hpp"
#include "../D.hpp"
#include "../M.hpp"
#include "../B.hpp" 
#include <vector>

using namespace <name>;

class A: public dec
{
protected:
    M* mr;
    std::vector<D*>* procRDC;

public:
    A(D* low, D* mid, D* high, M* marketRound);

protected:
    std::vector<B*>* split(D* d);

}; // class A

#endif

Base class dec.cpp contains an empty constructor I also give you D.cpp

#include "../D.hpp"

using namespace <name>;

D::D(std::vector<B*>* b) 
{
    bs = b;
}

std::vector<B*>* D::getB()
{
    return bs;
}

and D.hpp

#ifndef D_HPP
#define D_HPP

#include "../B.hpp"
#include <vector> 

using namespace <name>;

class D: public C
{
protected:
    std::vector<B*>* bs;

public:
    D(std::vector<B*>* b);
    std::vector<B*>* getB();

}; // class D

#endif

Class C only contains an empty constructor

B.cpp

#inlcude "../B.hpp"

using namespace <name>

B::B(int a1, int a2, int a3)
{
    a1 = a1;
    a2 = a2;
    a3 = a3;
}

int B::getA() { return a1;  }

B.hpp

#ifndef B_HPP
#define B_HPP

#include "../O"

using namespace <name>

class B : public O
{
protected:
    int a1;
    int a2;
    int a3;

public:
    B(int a1, int a2, int a3);

public:
    int B::getA();

};

#endif

Now it gives an error that it cannot find D::D referenced in function A::A, and some errors that it cannot find B::B in both A::A and D::D. I already tried adding a main function, eliminating the base class definition, double-checked the inclusion of header file... When selecting "jump to declaration" or "jump to definition" in Visual Studio 10, it can find the exact D::D function. I opened the header files to see if they were directing to the correct file and it does.

Do you guys have any suggestions where to look next to eliminate the error? Do you spot the mistake somewhere? I searched too long to figure it out on my own. Help is very much appreciated! Btw, I had to change the actual naming of the classes, but I checked whether the pseudo-names were correctly assigned. If other files are needed to clarify the mistake, please let me know and I will happily put them here.

Marijn
  • 21
  • 2
  • Oh crap, a header file for C should be added in D.hpp. This is the case in the real code but I eliminated it by accident here, so adding the C.hpp file is not a solution to the problem – Marijn Mar 09 '12 at 19:27

1 Answers1

0

Is D.cpp definitely included in your build? If D.cpp were missing, then you'd get this error.

To check for sure, try adding definitions for D in D.hpp:

class D: public C
{
protected:
    std::vector<B*>* bs;

public:
    D(std::vector<B*>* b) : bs(b) {}
    std::vector<B*>* getB() { return bs; }

}; // class D

If this fixes the linker error, you're not including D.cpp in your build.

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • I tried your suggestion but it did not solve the error. The cpp is also visible in the source files. – Marijn Mar 10 '12 at 17:05
  • Your example works for me (with a couple of minor tweaks); I can instantiate A, B and D objects. You must be changing something when you simplify your actual code to the sample. When you edited your question, you mentioned further errors; are these `LNK2019` errors also? – Fraser Mar 11 '12 at 00:15
  • They are all LNK2019 errors all relating back to class B or D. The only thing I did not elaborate here was the base class O, which is not empty but contains more or less the same methods as B. So, maybe this is rather an inheritance problem... Let me try and empty the base class to see if it works for me tomorrow. If it does, I'll post a more specific question than the one above. If not, I know that the code above does not contain major errors. Thank you for your help! – Marijn Mar 11 '12 at 09:54