Search code examples
c++lnk2019

Resolving a LNK2019 error in main


I know there are many linkage error questions similar to mine, but I haven't been able to fix it with any of the responses. I've made sure to add the correct scope resolution operators and give my constructors and destructors bodies, but the error is still there. Can anyone give me a hint besides "this has already been answered"?

Xx.obj : error LNK2019: unresolved external symbol "public: __thiscall Y::Y(void)" (??0Yy@@QAE@XZ) referenced in function _main

This is the relevant code:

  #include <iostream>

class Xx
{
    X::X() {};
    X::~X() {};
}; 
class Y : public X
{
public: //added public
    Y::Y();
    Y::~Y();       
};

int main()
{
    X *x = new Y;
    Y *y = new Y; //turned new Y to new X
    delete &x; //added deletes
    delete &y; //added deletes
    return 0;
}

Solution

  • You need to define the constructor to Yy.

    You'll notice for Xx you've defined your constructor as follows:

    Xx::Xx() {};
    

    The curly braces are the body (or definition) of the method. Your declaration of the constructor for Yy looks as follows:

    Yy::Yy();
    

    You're missing your body (based on the code you've provided) and I assume you probably want to follow the same pattern from your Xx class. Simply change it to the following code:

    Yy::Yy() {};
    

    To wit, you're also missing the definition for your destructor (the method declared in Yy that starts with a ~) for Yy.