创建抽象的动态数组 class
Creating a dynamic array of an abstract class
我正在尝试创建抽象 class (CellPhone) 的动态数组,然后用 Cell1 和 Cell2 类型的不同 objects 填充它。
我尝试使用动态数组和向量,但都出现错误:
所有 classes 都已创建并运行,但主要是:
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
我已经查看了其他情况,但两种方式都出现错误?为什么?以及如何修复它?
编辑:这里有 class headers 供参考:
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();
};
单元格 2 引用了单元格 1,因为如果没有,主单元中会出现 class 重定义错误。
只需将 class Cell2 : CellPhone
替换为 class Cell2 : public CellPhone
。
否则,无法访问从 Cell2
到 CellPhone
的转换(如果未指定,继承是 private
)。
编辑:正如下面的评论,强烈建议您为 CellPhone
class 声明一个虚拟析构函数(建议您在某些时候专攻任何 class)。
我正在尝试创建抽象 class (CellPhone) 的动态数组,然后用 Cell1 和 Cell2 类型的不同 objects 填充它。
我尝试使用动态数组和向量,但都出现错误:
所有 classes 都已创建并运行,但主要是:
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
我已经查看了其他情况,但两种方式都出现错误?为什么?以及如何修复它?
编辑:这里有 class headers 供参考:
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();
};
单元格 2 引用了单元格 1,因为如果没有,主单元中会出现 class 重定义错误。
只需将 class Cell2 : CellPhone
替换为 class Cell2 : public CellPhone
。
否则,无法访问从 Cell2
到 CellPhone
的转换(如果未指定,继承是 private
)。
编辑:正如下面的评论,强烈建议您为 CellPhone
class 声明一个虚拟析构函数(建议您在某些时候专攻任何 class)。