gpt4 book ai didi

c++ - 隐式转换

转载 作者:行者123 更新时间:2023-11-28 08:30:57 24 4
gpt4 key购买 nike

我的程序是:-


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

}

任何人都可以解释错误消息的含义吗?

最佳答案

您可能使用的是旧编译器,因为您包含了 <iostream.h>而不是新标准 <iostream>而且您没有使用命名空间 std.
修复此问题后,添加行 using namespace std;在顶部并注释掉 clrscr() 你的代码在 MSVC8 上编译得很好。

你有明确的理由使用protected吗?偏差?如果没有,我建议使用 public取而代之的是偏差。 protected推导是一件非常复杂和不常见的事情。

关于c++ - 隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2147077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com