VC++ 6.0下:C++程序出错,求高人指点“哪里出错”了。。。谢谢!

代码如下:#include<iostream.h>#include<string.h>classStudent{public:Student(){}Student(int... 代码如下:
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student() {}
Student(int n,char* na,char s) {num=n;strcpy(name,na);sex=s;}
void in()
{ num=1001;
name="lee";
sex='M'; }
private:
int num;
char* name;
char sex;
};
class Teacher
{
public:
Teacher() {}
Teacher(int n,char* na,char s) {num=n;strcpy(name,na);sex=s;}
Teacher(Student& s) {num=s.num;strcpy(name,s.name);sex=s.sex;}
void out()
{ cout<<num<<" "<<name<<" "<<sex<<endl; }
private:
int num;
char* name;
char sex;
};

int main()
{ Student s;
Teacher(& s);
s.in();
s.out();
return 0;
}
错误提示:
--------------------Configuration: 4 - Win32 Debug--------------------
Compiling...
4.cpp
E:\C++\4.cpp(24) : error C2248: 'num' : cannot access private member declared in class 'Student'
E:\C++\4.cpp(15) : see declaration of 'num'
E:\C++\4.cpp(24) : error C2248: 'name' : cannot access private member declared in class 'Student'
E:\C++\4.cpp(16) : see declaration of 'name'
E:\C++\4.cpp(24) : error C2248: 'sex' : cannot access private member declared in class 'Student'
E:\C++\4.cpp(17) : see declaration of 'sex'
E:\C++\4.cpp(35) : error C2040: 's' : 'class Teacher &' differs in levels of indirection from 'class Student'
E:\C++\4.cpp(35) : error C2530: 's' : references must be initialized
执行 cl.exe 时出错.

4.obj - 1 error(s), 0 warning(s)
展开
 我来答
雨兰吟G
2012-04-22
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
Teacher(Student& s) {num=s.num;strcpy(name,s.name);sex=s.sex;}
这里,Student的num是private的,所以外面不可以访问。
Teacher(& s); 调用的时候写成Teacher(s);就行了。但是这里还有问题就是没有给这个变量命名。
所以写成Teacher t(s);
然后
s.in();
t.out();
追问
谢啦,但改后还有错啊:
error C2228: left of '.name' must have class/struct/union type
error C2228: left of '.sex' must have class/struct/union type
error C2248: 'num' : cannot access private member declared in class 'Student'
see declaration of 'num'
error C2248: 'name' : cannot access private member declared in class 'Student'
(等等。。)
执行 cl.exe 时出错. 1 error(s), 0 warning(s)
追答
#include 
#include
using namespace std;

class Student
{
public:
Student() {}
Student(int n,char* na,char s) {num=n;strcpy(name,na);sex=s;}
void in()
{ num=1001;
name="lee";
sex='M'; }
int num;
char* name;
char sex;
private:

};
class Teacher
{
public:
Teacher() {}
Teacher(int n,char* na,char s) {num=n;strcpy(name,na);sex=s;}
Teacher(Student& s) {
num=s.num;
name=new char[strlen(s.name)+1];
strcpy(name,s.name);
sex=s.sex;}
void out()
{ cout<<num<<" "<<name<<" "<<sex<<endl; }
private:
int num;
char* name;
char sex;
};

int main()
{ Student s;
s.in(); //我不确定你原来的用意,在s.in()调用之前s中的数据都没初始化。
Teacher t(s);
t.out();
return 0;
}
Truman198701
2012-04-23
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
把num改为public
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ddxxqq413
2012-04-23
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
把num改为public
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式