下面是我编写的一些代码的简化版本。到目前为止,此代码工作正常
类.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 就不会导致此错误。
所以我的问题是:出了什么问题以及如何解决?