帮我解决错误,请告诉我我的错误。 [英] Help me solving the error and plz let me know my mistake.

查看:69
本文介绍了帮我解决错误,请告诉我我的错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不断引发错误,引用名称& roll是模糊的。



我尝试过的事情:



  #include   <   iostream  >  
#include < conio.h >
#include < string.h >
使用 命名空间标准;

class 学生
{
受保护
int roll,age;
char name [ 20 ];
public
void get()
{
cout<< 输入学生姓名;
cin>> name;
cout<< 输入滚动号<< name;
cin>> roll;
cout<< 输入年龄<< name;
cin>>年龄;
}
};

class 主题: public 学生
{
受保护
int p,c,m,h,e;
public
get_marks()
{
cout<< 输入物理标记;
cin>> p;
cout<< 输入化学标记;
cin>> c;
cout<< 输入数学标记;
cin>> m;
cout<< 输入印地语的标记;
cin>> h;
cout<< 输入英文标记;
cin>> e;
}
};

class 体育:公开学生
{
受保护
int phy_edu;
public
get_sports()
{
cout<< 输入体育标记;
cin>> phy_edu;
}
};

class 结果: public 主题, public Sports
{
protected
int total;
float avg;
char 等级[ 5 ];
public
calculate()
{
total = p + c + m + h + e;
avg =(总数/ 6);

if (avg> = 90
strcpy( grade, A +);
else if (avg> = 80
strcpy(年级, A);
else if (avg> = 70
strcpy(年级, A - );
else if (avg> = 60
strcpy(年级, B +);
else if (avg> = 50
strcpy(年级, B);
else if (avg> = 40
strcpy(年级, B - );
else if (avg< 40
strcpy(年级, 失败);
}

void display()
{
cout<< name<< < span class =code-string> \t<< roll<< \t<<总计<< \t<< avg<< \t<<年级;
}
};

int main()
{
Subject s;
s.get();
体育s1;
s1.get();
cout<< 名称\t卷号\ t总标记\t平均标记\t等级;
结果r;
r.display();
getch();
}

解决方案

您已经声明了一个所谓的钻石继承模式的教科书示例。结果来自主题和体育,两者都来自学生。在这种情况下,编译器不会访问哪些基类成员。这是一个很好的教程,说明它是什么以及如何解决它: C ++中的虚拟继承,并解决钻石问题 - Cprogramming .com [ ^ ]。

it keeps throwing error that reference to name & roll is ambiguous.

What I have tried:

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

class Student
{
protected:
	int roll,age;
	char name[20];
public:
	void get()
	{
		cout<<"Enter name of the student";
		cin>>name;
		cout<<"Enter roll no. of "<<name;
		cin>>roll;
		cout<<"Enter age of "<<name;
		cin>>age;
	}
};

class Subject: public Student
{
protected:
	int p,c,m,h,e;
public:
	get_marks()
	{
		cout<<"Enter marks of Physics";
		cin>>p;
		cout<<"Enter marks of Chemistry";
		cin>>c;
		cout<<"Enter marks of Maths";
		cin>>m;
		cout<<"Enter marks of Hindi";
		cin>>h;
		cout<<"Enter marks of English";
		cin>>e;
	}
};

class Sports: public Student
{
protected:
	int phy_edu;
public:
	get_sports()
	{
		cout<<"Enter marks of Physical Education";
		cin>>phy_edu;
	}
};

class Result: public Subject,public Sports
{
protected:
	int total;
	float avg;
	char grade[5];
public:
	calculate()
	{
		total = p+c+m+h+e;
		avg = (total/6);

		if(avg >= 90)
			strcpy(grade,"A+");
		else if(avg >= 80)
			strcpy(grade,"A");
		else if(avg >= 70)
			strcpy(grade,"A-");
		else if(avg >= 60)
			strcpy(grade,"B+");
		else if(avg >= 50)
			strcpy(grade,"B");
		else if(avg >= 40)
			strcpy(grade,"B-");
		else if(avg < 40)
			strcpy(grade,"Fail");
	}

	void display()
	{
		cout<<name <<"\t"<<roll <<"\t"<< total <<"\t"<< avg <<"\t"<< grade;
	}
};

int main()
{
	Subject s;
	s.get();
	Sports s1;
	s1.get();
	cout<<"Name \t Roll No. \t Total Marks \t Average Marks \t Grade";
	Result r;
	r.display();
	getch();
}

解决方案

You have declared a textbook example of what is known as the diamond inheritance pattern. Result is derived from Subject and Sports and both of those are derived from Student. The compiler doesn't which base class members to access in this situation. Here is a pretty good tutorial on what it is and how to fix it : Virtual Inheritance in C++, and solving the diamond problem - Cprogramming.com[^].


这篇关于帮我解决错误,请告诉我我的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆