有一个关于类的C++程序,编译没错,运行却一直出错,这是书本的代码,不知道哪里出错了
#include<string>#include<iostream>usingnamespacestd;classCStudent{public:voidset(char...
#include<string>#include<iostream>
using namespace std;
class CStudent
{
public:
void set(char*setname,char *setnum,int setage);
private:
char *name;
char *num;
int age;
float score;
};
void CStudent::set(char *setname,char*setnum,int setage)
{
name=new char[strlen(setname)+1];
if(name!=0)
strcpy(num,setname);
num=new char[strlen(setnum)+1];
if(num!=0)
strcpy(num,setnum);
age=setage;
}
int main(){
CStudent s1;
s1.set("zhanghua","1002",20);
return 0;
} 展开
using namespace std;
class CStudent
{
public:
void set(char*setname,char *setnum,int setage);
private:
char *name;
char *num;
int age;
float score;
};
void CStudent::set(char *setname,char*setnum,int setage)
{
name=new char[strlen(setname)+1];
if(name!=0)
strcpy(num,setname);
num=new char[strlen(setnum)+1];
if(num!=0)
strcpy(num,setnum);
age=setage;
}
int main(){
CStudent s1;
s1.set("zhanghua","1002",20);
return 0;
} 展开
展开全部
这是C还是C++?回楼下,你那是C型的,人家写的是C++行的好不?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<string>
#include<iostream>
头文件就错了
string.h
iostream.h
#include<iostream>
头文件就错了
string.h
iostream.h
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void CStudent::set(char *setname,char*setnum,int setage)
{
name=new char[strlen(setname)+1];
if(name!=0)
strcpy(name,setname);
num=new char[strlen(setnum)+1];
if(num!=0)
strcpy(num,setnum);
age=setage;
}
------------------
只能说你抄错了。。
strcpy(name,setname);
{
name=new char[strlen(setname)+1];
if(name!=0)
strcpy(name,setname);
num=new char[strlen(setnum)+1];
if(num!=0)
strcpy(num,setnum);
age=setage;
}
------------------
只能说你抄错了。。
strcpy(name,setname);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询