views:

106

answers:

2

My program was:-


#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();

}

CAN anyone explain what the error msg means?

+1  A: 

You are probably using an old compiler as you're including <iostream.h> instead of the new standard <iostream> and you aren't using namespace std.
After fixing this, adding the line using namespace std; on top and commenting out clrscr() your code compiles fine on MSVC8.

Do you have a clear reason to use protected derviation ? If not, I'd suggest using public derviation instead. protected derviation is something really quite complicated and uncommon.

abenthy
nothing woks though.... n im using turbo c++ - school requirements
Can't you use another compiler? On linux there is gcc and on windows there is msvc8, which are both free and up to date.
abenthy
A: 

When you derive a base class using protected or private then your derived class is not treated as base class and compiler will not perform implicit type conversion in that case.

Derived class in not base class in case of private or protected inheritance. whereas in case of public inheritance every derived class is a base class .

Try out explicit type casting in the function or make inheritance public.

Ashish