首页 新闻 会员 周边

帮我看个关于static的函数,

0
悬赏园豆:10 [待解决问题]

//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 个无法解析的外部命令

6745的主页 6745 | 初学一级 | 园豆:72
提问于:2012-01-11 22:28
< >
分享
所有回答(1)
1

h1::yy = 777;放到函数外定义就行

2012 | 园豆:21230 (高人七级) | 2012-01-12 08:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册