c++ - "unresolved external symbol"从一个类引用另一个类

标签 c++ visual-studio-2012

我正在尝试做一些我认为应该很容易的事情(作为 C++ 菜鸟):从一个类引用另一个类。出于某种原因,我收到一个错误:

Error 6 error LNK2019: unresolved external symbol "public: float * __thiscall Foo::Test(void)" (?Test@Foo@@QAEPAMXZ) referenced in function "void __cdecl Test(void)" (?Test@@YAXXZ) Bar.obj

Foo.h

class Foo
{
public:
    Foo(void);
    ~Foo(void);
    float* Test();
};

Foo.cpp

#include "Foo.h"
float* Test()
{
    return new float[0];
}

Bar.h

class Bar
{
public:
    Bar(void);
    ~Bar(void);
    void Test();
};

Bar.cpp

#include "Bar.h"
#include "Foo.h"
void Test()
{
    Foo* foo = new Foo();
    foo->Test();
}

为什么编译器不允许我从 Bar 引用类 Foo?我不知道这个错误是什么意思,也不知道如何调试它。

最佳答案

#include "Foo.h"
float* Test()
{
    return new float[0];
}

应该是:

#include "Foo.h"
float* Foo::Test()
{
    return new float[0];
}

您的版本只是定义了一个自由函数Test,与成员函数Foo::Test 不同。

一个 Unresolved external symbol 错误意味着编译器告诉你嘿,你告诉我我会找到这个东西的定义,但我看了看却找不到它。本质上,你对它撒了谎,它叫你出去。

其他垃圾是调用约定(__cdecl__thiscall)和损坏的(实际)函数名称(?Test@@YAXXZ ).

关于c++ - "unresolved external symbol"从一个类引用另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14449582/

相关文章:

c++ - 线程矩阵乘法

c++ - 转换一个值为 255 的 char 会导致 unsigned int 值为 4294967295,这是什么原因?

c++ - VC11 Winsock 应用程序不会关闭

c++ - Visual Studio 2012 - 两个项目 -> 链接器错误 (C++)

c++ - boost::dynamic_bitset 如何存储位

c++ - 使用 opencv 支持的 VLC 编译错误

c++ - 如何在常量数组中重载 "[][] operator"?

c - visual studio express 2012简单的c程序 fatal error LNK1561 : entry point must be defined

ajax - "Screen is not a member of My.MyComputer"

visual-studio - 为什么Visual Studio不喜欢这种XML模式? (前缀 ''不能映射到为 "xml"或 "xmlns"保留的 namespace 名称)