1

下面是我编写的一些代码的简化版本。到目前为止,此代码工作正常

类.h

namespace myNamespace
{
    class myClass
    {
    public:

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

    private:
        unsigned width;
        unsigned height;
    };
}

类.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 在 myNamespace 中的其他地方定义并包含在内)

使用 OtherClass 的构造函数不会改变 other,因此将其设为 const 是合适的。

但是,当我更改 .cpp 和 .h 中的构造函数以使用const OtherClass&它时,会出现错误:

错误 LNK2019:未解析的外部符号“公共:__thiscall myNamespace::myClass::myClass(class myNamespace::OtherClass &,unsigned int,unsigned int)”(??0CarbonMatrix@molecule@@QAE@AAVCarbonString@1@II@Z)在函数 _wmain path\main.obj 中引用

据我所知,只要在声明和定义中都使用 const 就不会导致此错误。

所以我的问题是:出了什么问题以及如何解决?

4

1 回答 1

2

该错误表明,您实际上没有更改头文件中的声明,或者在更改后没有重新编译所有源文件。

仔细检查您确实const OtherClass&在头文件中将其更改为。然后重新编译整个工程,即不仅是class.cpp,还有main.cpp。

于 2014-04-09T21:30:33.167 回答