【发布时间】:2016-11-03 14:15:41
【问题描述】:
直截了当: 我有2节课。一个存储一组信息,另一个存储不同的信息。
.h 文件
class Direction
{
private:
std::vector<float> dir;
public:
Direction(std::vector<float> objDir); ......};
.cpp 文件
#ifndef frw_dir_ed
#define frw_dir_ed
#include "frw_direction.h"
#endif
FRW::Direction::Direction(std::vector<float> objDir = { .0f, .0f })
{
for (int i = 0; i < 2; i++)
dir[i] = objDir[i];
}
Direction(float x, float y);
另一个 .h 文件:
class Position
{
private:
float posX;
float posY;
public:
Position(float x, float y);
另一个 .cpp 文件:
#ifndef frw_pos_ed
#define frw_pos_ed
#include "frw_position.h"
#endif // !frw_pos_ed
FRW::Position::Position(float x = 0, float y = 0)
{
setPos(x, y);
}
我还有第三节课。这个继承了之前的 2 个。
class gameObj : public Position, public Direction
{
public:
gameObj(float x, float y, std::vector<float> direction, bool renderable);
gameObj(float posX, float posY, float dirX, float dirY, bool renderable);
//some funcs, destructor
private:
FRW::Position objPos;
FRW::Direction objDir;
bool isRendered;
};
那是一个头文件,现在是 .cpp:
FRW::gameObj::gameObj(float x = 0.5f, float y = 0.5f, std::vector<float> direction = { .0f, .0f }, bool renderable = true)
: FRW::Direction::Direction(direction), FRW::Position::Position(x,y)
{
isRendered = renderable;
}
FRW::gameObj::gameObj(float posX = 0.5f, float posY = 0.5f, float dirX = .0f, float dirY = 0.f, bool renderable = true)
: FRW::Direction::Direction(dirX, dirY), FRW::Position::Position(posX, posY)
{
isRendered = renderable;
}
C2456 编译器错误:协构初始化器列表中的成员函数或嵌套类 错误 3 错误 C2535: 'void FRW::gameObj::__dflt_ctor_closure(void)' : 成员函数已定义或声明。
请问,谁能告诉我,我错过了 OOP 的哪个概念?为什么我会收到此错误?
【问题讨论】:
-
在哪里你得到错误?您可以尝试创建一个Minimal, Complete, and Verifiable Example 并展示给我们吗?
-
不知道这个错误在说什么,但是为什么
gameObj既继承了其他类又将其他类存储为成员? -
有点离题,但您应该阅读何时使用继承。游戏对象是位置和方向吗?如果不是,那么它不应该从他们那里继承。
标签: c++ oop inheritance parameter-passing