【问题标题】:Constant reference parameter causing unresolved external symbol常量引用参数导致无法解析的外部符号
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我仔细检查了一遍。如果我在两边都不一样,它甚至不会让我编译。所以我确定它具有相同的参数。我不确定我是否编译了整个项目。我使用 VS 2013,只需按本地 Windows 调试器。
猜你喜欢
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 2018-01-16
  • 2018-02-21
  • 1970-01-01
  • 2011-03-21
相关资源
最近更新 更多