C++不允许使用不完整的类型,求指点

Jasonty 2015-01-29 09:30:36
// 10xt7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Teacher
{
public:
class Student;
Teacher();
Teacher(int nu, string na, char s){ num = nu; name = na; sex = s; }
Teacher operator= (Student &);
void display();
private:
int num;
string name;
char sex;
};

class Student
{
public:
Student(int nu, string na, char s){ num = nu; name = na; sex = s; }
friend Teacher;
private:
int num;
string name;
char sex;
};

Teacher Teacher::operator= (Student &stu1)
{
Teacher tea;
tea.num = stu1.num;//这里会显示不允许使用不完整的类型
tea.name = stu1.name;
tea.sex = stu1.sex;
return tea;
}

void Teacher::display()
{
cout << num << endl;
cout << name << endl;
cout << sex << endl;
}

int main()
{
Student stu(123,"Jason",'m');
Teacher tea;
tea = stu;
tea.display();
return 0;
}


代码如上,就是实现赋值号=的重载,一个类赋值到另一个类,实在看不出来,球指点一二
...全文
5818 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 15 楼 lincolnandlinda 的回复:

Teacher& Teacher::operator= (Student &stu1)
{
    num = stu1.num;
    name = stu1.name;
    sex = stu1.sex;
    return *this;
}
哦,眼拙,果然可以了,我那么写为什么不行?费解、、新手好苦恼啊
lincolnandlinda 2015-01-30
  • 打赏
  • 举报
回复

Teacher& Teacher::operator= (Student &stu1)
{
    num = stu1.num;
    name = stu1.name;
    sex = stu1.sex;
    return *this;
}
lin5161678 2015-01-30
  • 打赏
  • 举报
回复
是的 不完整类型是不能用的
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 10 楼 lincolnandlinda 的回复:
写错了, 应该是return *this.


也不对,写到这一步this里的值还是乱的,如图
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 5 楼 fly_dragon_fly 的回复:
Teacher也可以是个student, 它可以从student派生
嗯 正在看继承,这个程序的初中就是写个运算符重载
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 4 楼 lyyslsw 的回复:
为什么将定义统统放在头文件内呢? 除非想做内联,否则类的定义和声明还是分开。
你说的是什么意思?我初学者不太懂这个
lincolnandlinda 2015-01-30
  • 打赏
  • 举报
回复
写错了, 应该是return *this.
lincolnandlinda 2015-01-30
  • 打赏
  • 举报
回复

Teacher& Teacher::operator= (Student &stu1)
{
    num = stu1.num;
    name = stu1.name;
    sex = stu1.sex;
    return this;
}
改成这样
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 3 楼 jiht594 的回复:
为什么等号两边是2个不相关的类. 这样不行吧, 没看见过这样的.
就是为了重载赋值号,使类可以赋值
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 2 楼 derekrose 的回复:
看看有什么不同
首先把提前引用声明放在类外了,这点我没注意到,第二点是我Teacher默认构造函数没写{} 多谢,这下编译通过了,可是返回tea的时候又出问题了,经过断点调试,形参tea里可以被赋值,但是不能返回?再次请教下
Jasonty 2015-01-30
  • 打赏
  • 举报
回复
引用 1 楼 CharlesSimonyi 的回复:
把Student的声明放到Teacher前面,因为Teacher的Teacher operator= (Student &);方法用到了Student &作参数,则在Teacher operator= (Student &);之前必须能看到Student的完整定义才行
感谢回复,可是我在Teacher类里已经提前预声明了class Student,难道必须写完整?
fly_dragon_fly 2015-01-30
  • 打赏
  • 举报
回复
Teacher也可以是个student, 它可以从student派生
Johnblx 2015-01-30
  • 打赏
  • 举报
回复
为什么将定义统统放在头文件内呢? 除非想做内联,否则类的定义和声明还是分开。
__威少__ 2015-01-30
  • 打赏
  • 举报
回复
引用 16 楼 Jasonty 的回复:
[quote=引用 15 楼 lincolnandlinda 的回复:]

Teacher& Teacher::operator= (Student &stu1)
{
    num = stu1.num;
    name = stu1.name;
    sex = stu1.sex;
    return *this;
}
哦,眼拙,果然可以了,我那么写为什么不行?费解、、新手好苦恼啊[/quote] 你那种写法应该是被垃圾回收了,改为引用就可以了
jiht594 2015-01-29
  • 打赏
  • 举报
回复
为什么等号两边是2个不相关的类. 这样不行吧, 没看见过这样的.
derekrose 2015-01-29
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Student;
class Teacher
{
public:
	Teacher(){}
	Teacher(int nu, string na, char s){ num = nu; name = na; sex = s; }
	Teacher operator= (Student &);
	void display();
private:
	int num;
	string name;
	char sex;
};

class Student
{
public:
	Student(int nu, string na, char s){ num = nu; name = na; sex = s; }
	friend Teacher;
private:
	int num;
	string name;
	char sex;
};

Teacher Teacher::operator= (Student &stu1)
{
	Teacher tea;
	tea.num = stu1.num;//这里会显示不允许使用不完整的类型
	tea.name = stu1.name;
	tea.sex = stu1.sex;
	return tea;
}

void Teacher::display()
{
	cout << num << endl;
	cout << name << endl;
	cout << sex << endl;
}

int main()
{
	Student stu(123, "Jason", 'm');
	Teacher tea;
	tea = stu;
	tea.display();
	return 0;
}


看看有什么不同
encoderlee 版主 2015-01-29
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
	Student(int nu, string na, char s){ num = nu; name = na; sex = s; }
	friend class Teacher;
private:
	int num;
	string name;
	char sex;
};

class Teacher
{
public:
	Teacher(){};
	Teacher(int nu, string na, char s){ num = nu; name = na; sex = s; }
	Teacher operator= (Student &);
	void display();
private:
	int num;
	string name;
	char sex;
};

Teacher Teacher::operator= (Student &stu1)
{
	Teacher tea;
	tea.num = stu1.num;//这里会显示不允许使用不完整的类型
	tea.name = stu1.name;
	tea.sex = stu1.sex;
	return tea;
}

void Teacher::display()
{
	cout << num << endl;
	cout << name << endl;
	cout << sex << endl;
}

int main()
{
	Student stu(123,"Jason",'m');
	Teacher tea;
	tea = stu;
	tea.display();
	return 0;
}
把Student的声明放到Teacher前面,因为Teacher的Teacher operator= (Student &);方法用到了Student &作参数,则在Teacher operator= (Student &);之前必须能看到Student的完整定义才行

64,670

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧