实验环境:
OS: Window 7
IDE: Visual Studio 2010 Pro
以下代码虽然没有语法错误,但无法通过编译。
#include <iostream>
using namespace std;
class C
{
public:
void f() { cout << ++x <<endl; }
private:
static int x;
};
int main()
{
C c0;
c0.f();
system( "pause" );
return 0;
}
编译结果如下:
1>------ 已启动生成: 项目: Hello World, 配置: Debug Win32 ------
1>生成启动时间为 2011/8/18 14:57:03。
1>InitializeBuildStatus:
1> 正在对“Debug\Hello World.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1> main.cpp
1>ManifestResourceCompile:
1> 所有输出均为最新。
1>main.obj : error LNK2001: 无法解析的外部符号 "private: static int C::x" (?x@C@@0HA)
1>E:\Hello World\Debug\Hello World.exe : fatal error LNK1120: 1 个无法解析的