复制构造函数无法识别继承的成员
Copy constructor doesn't recognize inherited members
我有两个类:
class entity {
public:
SDL_Rect pos;
float x;
float y;
std::string spriteFile;
SDL_Surface * spriteHandle;
int rePos (int newX, int newY);
int move (float deltaX, float deltaY, bool check); // Move x & y the specified amounts, also moving pos.x and pos.y for every integer increase of x or y
int display (SDL_Surface * screenSurface); //Use SDL_BlipSurface to blip the image to the screen
int loadImage (SDL_PixelFormat * format); //Load the image using the spriteFile string and optimize it using the SDL_PixelFormat of the Screen
entity (std::string file, int w, int h);
entity () {};
~entity () { SDL_FreeSurface(spriteHandle);}
entity (const entity &old) : pos(old.pos), x(old.x), y(old.y), spriteFile(old.spriteFile) {
spriteHandle = new SDL_Surface(*spriteHandle);
}
};
class multiEntity: public entity {
/*
Use multiEntity rather than entity when multiple images need to be blipped to different
positions.
*/
private:
static std::vector<stringBoolPair> deconstructed;
public:
std::string entType;
multiEntity (std::string file, int w, int h, std::string enttype) {
entity(file, w, h);
entType = enttype;
bool found = false;
for (int i = 0; i < deconstructed.size(); i++) {
found = (enttype == deconstructed[i].str);
}
if (found) deconstructed.emplace_back(enttype, false);
}
multiEntity (const multiEntity& old) :
spriteFile(old.spriteFile),
x(old.x),
y(old.y),
spriteHandle(old.spriteHandle),
pos(old.pos),
entType(old.entType) {}
~multiEntity () {
for (int i = 0; i < deconstructed.size(); i++) {
if (deconstructed[i].str == entType) {
SDL_FreeSurface(spriteHandle);
deconstructed[i].Bool = true;
}
}
}
multiEntity& operator= (const multiEntity& old) {
spriteFile = old.spriteFile;
pos = old.pos;
x = old.x;
y = old.y;
entType = old.entType;
spriteHandle = old.spriteHandle;
return *this;
}
};
当我尝试编译包含此内容的代码时,我会收到一个错误消息,说class multiEntity does not have any field named 'pos'
。除entType
外,复制构造函数中的所有变量都会发生这种情况。我试图做的是使用相同的sdl_surface具有entity
s的向量。因此,我觉得我应该创建一个单独的类,其中每个具有相同entType
的对象的spriteHandle
值相同。这应该指向相同的图像,当我有75个我试图在屏幕上的图像实例时,这最有用。我想使用vector的构造函数布局,以便将信息复制到上面,而不是每次创建新的指针。
您无法在派生类的复制构造函数中初始化基类的成员;它们应通过基类的复制构造函数初始化。您应该只在成员的Intalizer列表中调用它:
multiEntity (const multiEntity& old) :
entity(old), // initialize entity's members via entity::entity(const entity&)
entType(old.entType) // initialize multiEntity's member entType
{}
如果基本类的复制构造函数不是您想要的,则可以在派生类的构造体主体中进行一些分配,例如
multiEntity (const multiEntity& old) :
// nothing specified, same as write entity() here
// entity's members will be initialized via entity::entity()
entType(old.entType) // initialize multiEntity's member entType
{
// perform assignment here
spriteFile = old.spriteFile;
x = old.x;
y = old.y;
spriteHandle = old.spriteHandle;
pos = old.pos;
}
相关文章:
- 为什么我可以通过SubBase类的公共继承方法打印出基类的私人继承成员
- 派生类调用使用非继承成员的继承函数
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- C :原始成员与继承成员的优先级
- 更改继承成员的地址
- C 是否可以不继承成员
- 使用模板的继承阴影继承成员
- 继承成员功能的模板专业化
- 不允许继承成员,为什么
- 派生模板类和继承成员变量的语法谜题
- 继承成员地址的C++标准
- 多级继承成员访问
- 访问专用模板的私有/继承成员变量
- 基模板继承成员,减少所需的类型
- 继承成员函数指针
- 模板专用化类不继承成员
- C++14正在初始化类构造函数初始化列表中的继承成员
- 具有专门初始化的继承成员变量
- 模板继承成员字段
- 访问数据成员的继承成员函数