静态函数链接器错误

时间:2013-05-14 04:55:15

标签: 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 ...

1 个答案:

答案 0 :(得分:3)

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

int Example::Value;