0

Consider the Interface class:

class Interface
{
public:

    virtual ~Interface(){}

    virtual void someMethod() = 0;
    virtual void someMethod2(){}
};

Where someMethod is pure virtual and someMethod2 is virtual Then two derived classes Derived

class Derived : public Interface
{
public:
    Derived(){ cout << "derived constructor";}

    ~Derived(){ cout << "derived destructor";}

    virtual void someMethod() override { cout << "derived someMethod";}
    virtual void someMethod2() { cout << "derived someMethod2";}

};

and DerivedB:

class DerivedB : public Interface
{
public:
    DerivedB(){ cout << "derivedB constructor";}

    ~DerivedB(){ cout << "derivedB destructor";}

    virtual void someMethod() override { cout << "derivedB someMethod";}
};

Which implements just the pure virtual method. In the main I'm doing the following:

vector<Interface *> myVector(4);


for(int i = 0; i < 4; i++){
    if(i%2 == 0){
        myVector[i] = new Derived();
    }else{
        myVector[i] = new DerivedB();
    }
}

When I try to build I get the following error:

error LNK2019: unresolved external symbol "public: __thiscall DerivedB::DerivedB(void)" (??0DerivedB@@QAE@XZ) referenced in function _main
debug\Test.exe : fatal error LNK1120: 1 unresolved externals

What is wrong?

paperox
  • 11
  • 1
  • 4
  • 1
    and where your implementation of `DerivedB::DerivedB` ? implement it and error is disappear – RbMm Jul 12 '17 at 10:22
  • I already implemented the `DerivedB` constructor in the .cpp. I updated the original post. – paperox Jul 12 '17 at 10:24

1 Answers1

0

You should implement also other methods for derived classes you declare, namely Derived::~Derived(void), Derived::someMethod(void), Derived::someMethod2(void), DerivedB::~DerivedB(void) and DerivedB::someMethod(void).

See this post: What is the difference between a definition and a declaration? and What is an undefined reference/unresolved external symbol error and how do I fix it?

vasek
  • 2,759
  • 1
  • 26
  • 30