Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用父类函数的子类_C++ - Fatal编程技术网

C++ C++;使用父类函数的子类

C++ C++;使用父类函数的子类,c++,C++,我这里有点问题 车辆为父级 汽车和摩托车是儿童班 这是我的主要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

我这里有点问题

  • 车辆为父级
  • 汽车和摩托车是儿童班
这是我的主要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;
}
这里的问题是setName是车辆的功能,而不是汽车的功能。但是我做了应该继承函数的继承。但似乎不起作用

它说……请求vehicletwod[vehicleCounter]中的setName’,这是非类类型vehicletwod*

上述问题已解决

附加问题:

好的,我修改了之前的问题。至->

这是另一个问题

正如代码所示。在这方面

 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的函数

不管它怎么说。。车辆没有名为“设定点”的成员

在做了约翰提到的事之后。。上述问题也已解决。

但最困难的部分是如何检索已设置的内容,因为它是vehicletwod对象而不是car对象

假设在设定点之后,我想获取点()

我试着这么做

vehicletwod[0]->getPoint()


我收到一个错误,说getPoint是非类类型“vehicletwod”

好的,但我仍然不知道什么是
vehicletwod
,但如果您编写

vehicletwod[vehicleCounter]->setName(vehicleName);
而不是

vehicletwod[vehicleCounter].setName(vehicleName);
您有一个指针数组,因此需要使用
->

另一个问题是,
设定点
只是
汽车
的一个成员,
不是汽车
,因此在将其添加到数组之前,必须在汽车上调用它

像这样的

if (vehicleName == "Car") {
    car->setPoint();
    vehicletwod[vehicleCounter] = car;
    vehicletwod[vehicleCounter]->setName(vehicleName);
}

您必须以不同的方式实例化汽车和摩托车:

VehicleTwoD *car = new Car();
VehicleTwoD *motor = new Motorcycle();
然后将要在子类中使用的方法声明为受保护

不客气


PD:汽车不是VehicleTwoD,分类学很重要。

VehicleTwoD[vehicleCounter]是一个指针,所以需要取消对它的引用。尝试vehicletwod[vehicleCounter]->setName(vehicleName)


你可能想仔细阅读指针。Google“c++类指针基础知识”或checkout

似乎
VehicleTwoD
没有这样的方法:

protected:
virtual void setPoint();

由于数组的定义,您需要此设置。

VehicleTwoD没有设置点(),因此您必须将VehicleTwoD*动态转换为Car*,但您必须确保VehicleTwoD[vehicleCounter]确实指向Car的对象,否则这是一个运行时错误

dynamic_cast<Car*>(vehicletwod[vehicleCounter])->setPoint();

在派生类中实现设定点()。

什么是VehicleTwoD?Car是否继承自VehicleTwoD?好的,问题似乎真的是什么
VehicleTwoD
,尽管现在至少有三个请求,但您仍然没有告诉我们。好的,请参见下面我编辑的答案。我有,但附加问题不在那里:)添加后,我如何检索它?谢谢,这解决了问题,你能看看上面的附加问题吗?这又解决了:)你是最好的。但现在的问题是,既然我添加了所有内容以假设vehicletwod[0],那么假设我得到了一个函数调用getPoint(),我该如何设置我的点哪个返回点结构我不能调用getPoint,因为它不能工作,因为它说它是非类类型的vehicletwod@user1777711这是个问题。忘记C++一分钟。你有各种各样的车辆,汽车,摩托车等等,但只有汽车有一个点。如果你试图从一辆不是汽车的汽车上获得分数,你期望会发生什么?除非你回答这个问题,否则不可能说出C++中正确的事情是什么。另一种说法是,如果在您将汽车添加到车辆阵列之后,我们希望再次将其视为汽车,那么将汽车添加到车辆阵列的意义何在?这些是设计问题,不是编程问题。
dynamic_cast<Car*>(vehicletwod[vehicleCounter])->setPoint();
class VehicleTwoD {
public:
    virtual void setPoint() = 0;
    /***/
};