//h1.h
#pragma once
# include <string>
using namespace std;
class h1
{
public:
h1(string a = "fileone",int b = 5);
static void output();
string filename;
int tt;
static int yy;
};
//h1.cpp
#include "h1.h"
# include <iostream>
# include <string>
using namespace std;
h1::h1(string a,int b)
{
filename = a;
tt = b;
}
void h1::output()
{
cout << yy << endl;
}
//c1.cpp
# include "h1.h"
# include <iostream>
# include <string>
using namespace std;
int main()
{
h1 file_1("5ID",100);
h1::yy = 777;
file_1.output();
system("pause");
return 0;
}
报错如下
1>c1.obj : error LNK2001: 无法解析的外部符号 "public: static int h1::yy" (?yy@h1@@2HA)
1>h1.obj : error LNK2019: 无法解析的外部符号 "public: static int h1::yy" (?yy@h1@@2HA),该符号在函数 __ehhandler$??0h1@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z 中被引用
1>D:\C++ workspace\构造函数\Debug\构造函数.exe : fatal error LNK1120: 1 个无法解析的外部命令
h1::yy = 777;放到函数外定义就行