Question

Below is a simplified version of some code I wrote. this code works fine so far

class.h

namespace myNamespace
{
    class myClass
    {
    public:

        myClass(unsigned width, unsigned height);
        myClass(OtherClass& other, unsigned width, unsigned height);
        ~myClass(){};

    private:
        unsigned width;
        unsigned height;
    };
}

class.cpp

#include "class.h"

namespace myNamespace
{
    myClass::myClass(unsigned width, unsigned height)
    {
        //code
    }

    myClass::myClass(OtherClass& other, unsigned width, unsigned height) : myClass(width, height)
    {
        //code  
    }
}

(OtherClass is defined elsewhere inside the myNamespace and is included)

the constructor that uses OtherClass doesn't alter other so making it const would be appropriate.

But when I change the constructor in both the .cpp and .h to use const OtherClass& it gives me the error:

error LNK2019: unresolved external symbol "public: __thiscall myNamespace::myClass::myClass(class myNamespace::OtherClass &,unsigned int,unsigned int)" (??0CarbonMatrix@molecule@@QAE@AAVCarbonString@1@II@Z) referenced in function _wmain path\main.obj

As far as I know const shouldn't cause this error as long you use them both at the declaration and at the definition.

So my question: What goes wrong and how to fix it?

Was it helpful?

Solution

That error indicates, that you have actually not changed the declaration in the header file or did not recompile all source files after changing it.

Double check that you indeed changed it to const OtherClass& in the header file. Then re-compile the whole project, i.e. not only class.cpp, but also main.cpp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top