c++ - 未解析的外部符号 (LNK2019)

标签 c++ visual-studio-2008 linker

<分区>

我有以下文件:
接口(interface).h

class Interface
{
  ...
};

Interface* CreateInterface();

实现.h

#include "interface.h"

class Impl: public Interface
{
  ...
};

实现.cpp

#include "impl.h"

SomeInterface* CreateInterface()
{
  return new Impl;
}
...

主要.cpp

#include "Interface.h"
int main()
{
  CreateInterface();
}

最初我的项目看起来很喜欢 this .但出于测试目的,所有文件现在都在同一个项目中。
编译时出现 LNK2019 错误,提示 CreateInterface 未解析。我正在使用 VS 2008,我不知道出了什么问题。

最佳答案

您是否打算拥有多个 CreateInterface() 声明?

接口(interface).h

Interface* CreateInterface();

Impl.cpp

SomeInterface* CreateInterface()

其中只有一个(后者)似乎被实际实现了。您的名称含糊不清,编译器似乎选择了错误的名称(很可能是因为它甚至不知道另一个名称是否存在)。

在 impl.cpp 中使用 相同 参数列表和返回类型定义 CreateInterface() 作为 Interface.h 中的原型(prototype)。我通常不会容忍这种野蛮的设计,但看起来这就是你想要的。

Impl.cpp

Interface* CreateInterface() // note the return type
{
    return new Impl;
}

老实说,我不敢相信这甚至可以编译,考虑到你只是两者之间的区别(Interface.h 中的声明和 Impl.cpp 中的实现)是返回类型的区别,编译器应该吐个遍那,所以您可能还没有向我们展示一些东西。

关于c++ - 未解析的外部符号 (LNK2019),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13560625/

相关文章:

c++ - 在自己的声明中使用类作为模板参数

visual-studio-2008 - 如何阻止 VS2008 错误列表中出现 CSS 验证错误

c - 什么浮点值使 sprintf_s() 产生 "1.#QO"?

linux - ELF 标准和重定位偏移量计算

c - 无法链接到 fftw3 库

c++ - 为什么我会收到 Unresolved external 问题?

c++ - 是否可以在不使用 virtual 关键字的情况下调用(同名)子函数?

c++ - 在arduino中解析canbus数据的最佳方法?

visual-studio-2008 - 我能否以编程方式从 Visual Studio 2008 中导出类图?

c++ - protocol buffers + zlib = 未解析的外部符号