ここで少し問題が発生しました。
- 車両は親クラスです
- 車とオートバイは子供クラスです
これは私のメインのcppファイルです
int main() {
// assuming max size is up to 100
VehicleTwoD *vehicletwod[100];
int vehicleCounter = 0;
Car *car = new Car();
Motorcycle *motor = new Motorcycle();
cout << " Please enter name of vehicle ";
cin >> vehicleName;
if (vehicleName == "Car") {
vehicletwod[vehicleCounter] = car;
//issue here
vehicletwod[vehicleCounter].setName(vehicleName);
//issue end
}
return 0;
}
これは私のcar.cppです
struct Point {
int x, y;
};
class Car: public Vehicle {
private:
Point wPoint[4];
double area;
double perimter;
public:
void setType(string);
void setPoint();
string getType();
string getPoint();
}
ここでの問題は、setNameがVehicleの関数であり、Carの関数ではないことです。しかし、私は関数を継承する必要がある継承を行いました。しかし、うまくいかないようです。
それは言う...非クラスタイプVehicleTwoD*であるVehicletwod[vehicleCounter]'のsetNameの要求
上記の問題は修正されました
追加の問題:
さて、を変更して前の問題を修正しました。〜>
ここで他の問題。
コードがそうであるように。この部分で
if (vehicleName == "Car") {
vehicletwod[vehicleCounter] = car;
vehicletwod[vehicleCounter]->setName(vehicleName);
//now i want to setPoint which is a function of child class
vehicletwod[vehicleCounter]->setPoint();
//issue here
}
子クラスCarの関数であるsetPointを試してみます
ただし、車両には「setPoint」という名前のメンバーがありません。
ジョンが言ったことをした後..上記の問題も修正されました..
しかし、最も難しいのは、車のオブジェクトではなく、Vehicletwodオブジェクトであるため、設定されたものを取得する方法です。
setPointの後で、getPoint()を取得したいとします。
私はこれをやってみます
Vehicletwod [0]-> getPoint();
getPointが非クラスタイプの「vehicletwod」であるというエラーが表示されます