每个人!有如下代码片段: testcase.cpp
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
static int b ;
static void test()
{
b = 3;
cout << b<<endl;
}
};
int main()
{
Test::test();
return 0;
}
当我单击“构建”按钮时,输出消息是
错误 LNK2001:无法解析的外部符号“public:static int Test::b”(?b@Test@@2HA) 1>B:\PROGRAMPROJECT\visual 2015 pro\testcase\testcase\x64\Debug\testcase.exe:致命错误LNK1120:1 个未解决的外部因素
但是,当我像这样更改代码位置时:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
//static int b;
static void test()
{
static int b;//only change this line
b = 3;
cout << b << endl;
}
};
int main()
{
Test::test();
return 0;
}
它确实有效!我不知道为什么?有人可以帮助我吗?我的IDE是vs pro 2015 + windows10。