gpt4 book ai didi

C++ 错误 : invalid conversion from Inheritance

转载 作者:行者123 更新时间:2023-11-28 00:27:27 25 4
gpt4 key购买 nike

<分区>

我是 C++ 新手。我正在学习 C++ 中的继承。我无法理解编译时错误。我有父类(super class) Measurable,Car 和 Country 派生自 Measurable。在可衡量的我有被子类覆盖的纯虚函数getMeasurable。我有数据类遍历可测量对象(汽车或国家)并计算平均值。

我遇到了错误

measure.cc: In function ‘int main()’:
measure.cc:70: error: invalid conversion from ‘Car**’ to ‘Measurable**’
measure.cc:70: error: initializing argument 1 of ‘static int Data::avg(Measurable**, int)’

代码

#include <iostream>
class Measurable ;

class Measurable {
public:
virtual int getMeasurable() = 0 ;
};

class Country: public Measurable {
private:
int size ;
public:
Country(int size): size(size) {
}
int getMeasurable() {
return size ;
}
};

class Car: public Measurable {
private:
int mileage ;
public:
Car(int size): mileage(size) {
}
int getMeasurable() {
return mileage ;
}
};

class Data {

public:
static int avg ( Measurable* obj[] , int num) {
double size = 0 ;
for (int i = 0 ; i < num ;i++)
{

size += obj[i]->getMeasurable() ;
}
return size/num ;
}
};

int main () {


Car* fleet[] = {
new Car (1) , new Car (2) , new Car (3) , new Car (4)
};

double sum = Data::avg (fleet ,4) ;
}

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