我已遵循这些说明:
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;
}
我该如何解决这个问题,以便能够打印 PrintA
和 PrintB
的值?我在 Mac OS X 10.6.8 和 Xcode 3.2 上...
最佳答案
在cpp文件中定义静态变量。
int Example::Value;
关于c++ - 静态函数链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535401/