看看这个C++程序为什么会错

来源:百度知道 编辑:UC知道 时间:2024/09/27 06:06:09
#include<iostream>
#include<cstdlib>
using namespace std;

class Interger
{ public:
Interger(int);

protected:
int i;
};

Interger::Interger(int t)
{ i=t;}

class Real
{ public:
Real(float);

protected:
float r;
};

Real::Real(float t)
{ r=t;}

class IntReal:public Interger,public Real
{
public:
IntReal(Interger a1);
IntReal(Real a2);

IntReal operator+(IntReal);
IntReal operator-(IntReal);
IntReal operator*(IntReal);
IntReal operator/(IntReal);
friend ostream &operator<<(ostream & output,IntReal & A);
protected:
double a;
};

IntReal::IntReal(Interger t)
{ a=double(t.i);}
IntReal::IntReal(Real t)
{ a=double(t.r);}

void main()
{ int a,b;
cout<<"please input two intergers:

在 using 这一行下面加上
class Interger;
class Real;
class IntReal;
friend ostream &operator<<(ostream & output,IntReal & A);

protect 成员,子类可以访问,但是外部不可以访问。
cannot access protected member declared in class 'Interger'

出现的问题是
Interger::Interger(int t)
{ i=t;}

外部函数怎么能访问受保护的成员!

华工的吧,我也是。刚也正在写这个作业,还没写完。问题和你的差不多。我在基类里没定义为private 定义为public了。虽然我知道这样不好。但是起码可以编译通过

把基类里成员改成protect:,而不是private:,试一试,其他没看