Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++_Arrays_Class_Dynamic_Abstract - Fatal编程技术网

C++ 创建抽象类的动态数组

C++ 创建抽象类的动态数组,c++,arrays,class,dynamic,abstract,C++,Arrays,Class,Dynamic,Abstract,我试图创建一个抽象类(手机)的动态数组,然后用Cell1和Cell2类型的不同对象填充它 我尝试使用动态数组和向量,但都出现了错误: 所有类都是创建和工作的,但主要是: Cell1 c1("Orange", "Hello! This is your friend Rima, call me when you can.", 0777170, "Sony"); Cell2 c2("Zain", "Call me ASAP, Sam", 0777777777, "blue", "wifi"); Cel

我试图创建一个抽象类(手机)的动态数组,然后用Cell1和Cell2类型的不同对象填充它

我尝试使用动态数组和向量,但都出现了错误:

所有类都是创建和工作的,但主要是:

Cell1 c1("Orange", "Hello! This is your friend Rima, call me when you can.", 0777170, "Sony");
Cell2 c2("Zain", "Call me ASAP, Sam", 0777777777, "blue", "wifi");
Cell1 c3("Omnia", "Let me know when you can pass by", 0711111111, "Samsung");

CellPhone *c[3];

*c[0]=&c1;       //Conversion to base class error


vector<CellPhone*>  cp;
cp.push_back(&c1);      //Conversion to base class error
})

})

})


Cell2具有Cell1的引用,因为如果没有,则会在主目录中出现类重新定义错误。

只需将class
Cell2:mobile
替换为class
Cell2:public mobile

否则,无法访问从
Cell2
mobile
的转换(如果未指定继承是
private


编辑:正如下面所评论的,强烈建议您为
手机
类声明一个虚拟析构函数(这对于您在某一点上专门从事的任何类都是建议的)。

如果没有手机、Cell1、Cell2的定义,就不可能回答。如果不选择继承,C++默认为私有继承。这是不谨慎的。另外,你的
mobile
类缺少虚拟析构函数。另外,99%的可能性是你应该在mobile中添加虚拟析构函数。@Frank:如果你将派生对象存储在数组中,指向基类指针,那么这是100%的可能性。只有当他从向量中删除对象时,这才是真的,但事实并非如此,因为他在堆栈上创建了单元格对象。在少数情况下,虚拟析构函数是不需要/不需要的(通常需要一个受保护的析构函数来避免问题),但这里几乎肯定不是这种情况。@IInspectable:仅当您最终使用容器销毁对象时。正如OP所说,对象是作为局部变量在堆栈上创建的,它们会被很好地销毁。@PaulMcKenzie:罕见的边缘情况。这是语言在看到虚拟函数时不添加虚拟析构函数的原因。
 class CellPhone{
  private:
     string branch, message;
     int phoneNumber;
 public:
    CellPhone(string, string, int);
    virtual void receiveCall() = 0;
    void receiveMessage();
    virtual void dial() = 0;
    void setBranch(string);
    void setMessage(string);
    void setPhoneNumber(int);
    string getBranch();
    string getMessage();
    int getPhoneNumber();
  #include "CellPhone.h"

 class Cell1:CellPhone{
 private:
     string cameraType;
     bool isCameraUsed;
 public:
     Cell1(string, string, int, string);
     void capture();
     void receiveCall();
     void dial();
     void setCameraType(string);
     string getCameraType();
 #include "Cell1.h"

 class Cell2:CellPhone{
 private:
      string wifi, bluetooth;
public:
     Cell2(string, string, int, string, string);
void turnBluetoothOn();
void turnBlueToothOff();
void setWifi(string);
void setBluetooth(string);
string getWifi();
string getBluetooth();
void receiveCall();
void dial();