c++ - 描述中提到的代码的未解析外部符号

标签 c++ oop static

 #include <iostream>
class t1
{
public:
    ~t1();
    static t1& fun();
private:
     t1()
     { 
     }
};

t1& t1::fun()
{
    return t1();
}

int main()
{
    t1::fun();
    return 0;
}

我收到未解析的外部符号。请帮忙。错误如下

Error  2   error LNK2019: unresolved external symbol "public: __thiscall t1::~t1(void)" (??1t1@@QAE@XZ) referenced in function "public: static class t1 & __cdecl t1::fun(void)" (?fun@t1@@SAAAV1@XZ)  D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\TestWebServer\TestWebServer.obj    TestWebServer
Error   3   error LNK1120: 1 unresolved externals   D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\Debug\TestWebServer.exe    1   1   TestWebServer

最佳答案

定义构造函数和析构函数。

#include <iostream>
class t1

{

public:

    ~t1() {} // <<<< defined here

    static t1& fun();

private:

    t1() {} // << defined here

};

t1& t1::fun()

{

    return t1();

}

int main()

{

    t1::fun();

    return 0;

}

关于c++ - 描述中提到的代码的未解析外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40930494/

相关文章:

c++ - 删除元素后列表的最后一个元素损坏

c++ - 与 omp 并行

C++如何初始化包含互斥锁的类的静态变量

C++类和静态

C++ 使用可变参数模板绑定(bind)成员函数

c++ - GCC : literal string types in a structure 中的可能错误

java - 使用已检查的异常重写方法

c++ - 错误使用前向声明?

java - 为什么自定义异常 java 类应该是最终的

php - 严格标准:静态函数 model::tableStruct() 不应该是抽象的