c++ - 静态函数链接器错误

标签 c++ static linker

我已遵循这些说明:

is it possible to define the static member function of a class in .cpp file instead of its header file? http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-static-functions.html

但是,我的示例给出了链接器错误:

"Example::Value", referenced from:
Example::PrintA()     in Text.o
Example::PrintB()     in Text.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

这是我的 Text.h 文件:

class Example
{

public:

    static int Value;

public:

    static void PrintA();
    void PrintB();

};

还有我的 Text.cpp 文件:

void Example::PrintA()
{

    cout << Value;

}

void Example::PrintB()
{

    cout << Example::Value;

}

我该如何解决这个问题,以便能够打印 PrintAPrintB 的值?我在 Mac OS X 10.6.8 和 Xcode 3.2 上...

最佳答案

在cpp文件中定义静态变量。

int Example::Value;

关于c++ - 静态函数链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535401/

相关文章:

c++ - OpenGL 透明效果在 Meego 上显示的很糟糕

linker - 如何转储使用 osicat 的可执行 SBCL 镜像

c++ - 如何从静态类函数初始化静态类对象

java - 为什么这个静态java方法不是线程安全的?

linker - "Local Linker Symbols"和 "Local Program Variables"有什么区别?

使用 nvcc 编译依赖的 .c 和 .cu 文件

c++ - 禁用 GCC 自动并行化

c++ - 填充与矩形相交的蒙版轮廓

c++ - Sean Parent : for polymorphic types in an inheritance hierarchy, 具有可变对象是极端的异常(exception)

C# 创建静态类的全局实例