实现客户机(CLIENT)类(C++)
实现客户机(CLIENT)类。声明字符型静态数据成员ServerName,保存其服务器名称;声明整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数Ch...
实现客户机(CLIENT)类。声明字符型静态数据成员ServerName,保存其服务器名称;声明整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。在头client.h文件中声明类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况
我这样编老出现这个错误
client.h
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include<iostream>
using namespace std;
class CLIENT{
public:
static int c;
static char s[10];
static void ChangeServerName(){cout<<ServerName[10]<<endl;}
void Result1( int c);
void show1(){
cout<<c<<endl;}
void Result2( char s[10]);
void show2(){
cout<<s[10]<<endl;}
private:
static int ClientNum;
static char ServerName[10];
};
#endif
client.cpp
#include"client.h"
#include<iostream>
#include<string>
using namespace std;
int CLIENT::c=2;
char CLIENT::s[10]="abc";
void CLIENT::Result1( int c){
ClientNum=c;
}
void CLIENT::Result2( char s[10] ){
ServerName[10]=s[10];
}
lab5_2.cpp
#include"client.h"
#include<iostream>
using namespace std;
int main(){
CLIENT b;
cout<<"ServeeName:"<<endl;
b.show1();
cout<<"ClientNum:"<<endl;
b.show2();
cout<<"ChangeServerName:"<<endl;
CLIENT::ChangeServerName();
return 0;
}
出现错误:
client.obj : error LNK2001: unresolved external symbol "private: static int CLIENT::ClientNum" (?ClientNum@CLIENT@@0HA)
client.obj : error LNK2001: unresolved external symbol "private: static char * CLIENT::ServerName" (?ServerName@CLIENT@@0PADA)
lab5_2.obj : error LNK2001: unresolved external symbol "private: static char * CLIENT::ServerName" (?ServerName@CLIENT@@0PADA) 展开
我这样编老出现这个错误
client.h
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include<iostream>
using namespace std;
class CLIENT{
public:
static int c;
static char s[10];
static void ChangeServerName(){cout<<ServerName[10]<<endl;}
void Result1( int c);
void show1(){
cout<<c<<endl;}
void Result2( char s[10]);
void show2(){
cout<<s[10]<<endl;}
private:
static int ClientNum;
static char ServerName[10];
};
#endif
client.cpp
#include"client.h"
#include<iostream>
#include<string>
using namespace std;
int CLIENT::c=2;
char CLIENT::s[10]="abc";
void CLIENT::Result1( int c){
ClientNum=c;
}
void CLIENT::Result2( char s[10] ){
ServerName[10]=s[10];
}
lab5_2.cpp
#include"client.h"
#include<iostream>
using namespace std;
int main(){
CLIENT b;
cout<<"ServeeName:"<<endl;
b.show1();
cout<<"ClientNum:"<<endl;
b.show2();
cout<<"ChangeServerName:"<<endl;
CLIENT::ChangeServerName();
return 0;
}
出现错误:
client.obj : error LNK2001: unresolved external symbol "private: static int CLIENT::ClientNum" (?ClientNum@CLIENT@@0HA)
client.obj : error LNK2001: unresolved external symbol "private: static char * CLIENT::ServerName" (?ServerName@CLIENT@@0PADA)
lab5_2.obj : error LNK2001: unresolved external symbol "private: static char * CLIENT::ServerName" (?ServerName@CLIENT@@0PADA) 展开
1个回答
展开全部
int CLIENT::ClientNum=0;
char CLIENT::ServerName[10]={0}; 编译就过了~
char CLIENT::ServerName[10]={0}; 编译就过了~
更多追问追答
追问
什么意思??
追答
你的静态成员变量没有初始化~~
即:
private:
static int ClientNum;
static char ServerName[10];
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询