我遵循了这些指示:
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 ...
答案 0 :(得分:3)
在cpp文件中定义静态变量。
int Example::Value;