0

私のプログラムは: -


#include < iostream.h>
#include < conio.h>

struct base
{
  protected:

    void get()
    {
        cin>>a>>b;
    }

  public:

    base(int i=0, int j=0);

    void put()
    {
        cout << a << '\t' << b << "\tput 1";
    }

    int a,b,c;

    ~base()
    {
        cout << "base destroyed";
    }
};

class deri : protected base
{

    int c,d;
    char w;
    int ans;

  public:

    deri(int r=7, int s=0)
      : base(r,s)
    {
        c=r;
        d=s;
        cout << "\nDerived invoked\n";
    }

    void put()
    {
        cout << c << '\t' << d << '\t' << a << '\t' << b;
    }
};


class d2 : protected deri
{
  public:

    d2() {}
    void start();
    void add()
    {
        get(); // ERROR HERE: Implicit conversion of 'd2 *' to 'base *' not allowed

    }

    ~d2(){}
};

void d2::start()
{
    put();
}

base::base(int i, int j)
{
    a=i;
    b=j;

    cout << "\nbase invoked\n";
    cout << "Enter a,b: ";
    get();
}


void main()
{
    clrscr();
    getch();

}

エラーメッセージの意味を説明できる人はいますか?

4

2 に答える 2

1

<iostream.h>新しい標準の代わりにインクルードしてい て、<iostream>ネームスペースを使用していないため、おそらく古いコンパイラを使用しています。std.
using namespace std;

偏差を使用する明確な理由はprotectedありますか? そうでない場合は、public代わりに偏差を使用することをお勧めします。protected偏差は非常に複雑で珍しいものです。

于 2010-01-27T13:28:10.453 に答える
0

protected または private を使用して基本クラスを派生させると、派生クラスは基本クラスとして扱われず、その場合、コンパイラは暗黙的な型変換を実行しません。

プライベートまたは保護された継承の場合、基底クラスではない派生クラス。一方、パブリック継承の場合、すべての派生クラスは基本クラスです。

関数で明示的な型キャストを試すか、継承を公開してください。

于 2010-01-27T13:29:45.933 に答える