【发布时间】:2014-05-23 08:18:03
【问题描述】:
以下是我编写的一些代码的简化版本。到目前为止,这段代码运行良好
类.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 在 myNamespace 的其他地方定义并包含在内)
使用 OtherClass 的构造函数不会改变 other,因此将其设为 const 是合适的。
但是当我将 .cpp 和 .h 中的构造函数更改为使用 const OtherClass& 时,它给了我错误:
错误 LNK2019:未解析的外部符号“公共:__thiscall myNamespace::myClass::myClass(class myNamespace::OtherClass &,无符号 整数,无符号整数)" (??0CarbonMatrix@molecule@@QAE@AAVCarbonString@1@II@Z) 中引用 函数_wmain路径\main.obj
据我所知,只要您在声明和定义中都使用 const 就不会导致此错误。
所以我的问题是:出了什么问题以及如何解决?
【问题讨论】:
-
你的问题是什么?
标签: c++ reference constants unresolved-external const-reference