C++ 复制ctor和ctor的未定义引用
我在此文件中有一个基类:C++ 复制ctor和ctor的未定义引用,c++,constructor,copy,undefined,C++,Constructor,Copy,Undefined,我在此文件中有一个基类: #ifndef MTM_CHARACTER_H_ #define MTM_CHARACTER_H_ #include "Exceptions.h" #include "Auxiliaries.h" #include <memory> namespace mtm { class Character { protected: int health
#ifndef MTM_CHARACTER_H_
#define MTM_CHARACTER_H_
#include "Exceptions.h"
#include "Auxiliaries.h"
#include <memory>
namespace mtm
{
class Character
{
protected:
int health
public:
Character(int health): health(health){}
virtual Character* clone() const = 0;
};
}
#endif
我在这个网站上查找了关于这类问题的所有问题,但没有一个解决了这个问题
我用狙击手的方式
std::共享(新狙击手(健康));std::共享其他(新狙击手(健康))代码>
我使用克隆的方式:
ptr=static_cast(其他_ptr->clone())代码>看起来您可能正在实例化一个抽象类(因为virtual方法是纯virtual(=0))
确保您正在动态实例化虚拟类型,而不是使用newcharacter()
——因为这会命名抽象类型
所以不要:
Character x(.....);
也不要:
Character x = Sniper(....); // slicing to abstract base
注:
- 您不在构造函数中初始化
团队
,而且为空
也是未使用的
工作样本:
using units_t = int;
enum class Team {team1};
enum class CharacterType {hulk};
#ifndef MTM_CHARACTER_H_
#define MTM_CHARACTER_H_
//#include "Exceptions.h"
//#include "Auxiliaries.h"
#include <memory>
namespace mtm {
class Character {
protected:
units_t health;
units_t ammo;
units_t range;
units_t power;
Team team;
CharacterType character_type;
public:
Character(units_t health, units_t ammo, units_t range, units_t power,
Team team, CharacterType type)
: health(health), ammo(ammo), range(range), power(power),
team(team), character_type(type) {}
virtual Character* clone() const = 0;
Character(const Character& character) = default;
};
} // namespace mtm
#endif
#ifndef MTM_SNIPER_H_
#define MTM_SNIPER_H_
//#include "Character.h"
#include <memory>
namespace mtm {
class Sniper : public Character {
private:
bool empty = false;
units_t successful_attack = 0;
public:
Sniper(units_t health, units_t ammo, units_t range, units_t power,
Team team, CharacterType type, bool empty = false,
units_t successful_attack = 0)
: Character(health, ammo, range, power, team, type),
empty(empty), successful_attack(successful_attack) {}
virtual Character* clone() const override { return new Sniper(*this); }
Sniper(const Sniper& sniper) = default;
};
} // namespace mtm
#endif
int main() {
std::unique_ptr<mtm::Character> ch;
ch = std::make_unique<mtm::Sniper>(1,1,1,1,Team::team1,CharacterType::hulk);
}
使用单位\u t=int;
枚举类团队{team1};
枚举类CharacterType{hulk};
#ifndef MTM_字符_
#定义MTM_字符_
//#包括“Exceptions.h”
//#包括“附件h”
#包括
名称空间mtm{
类字符{
受保护的:
单位卫生;
单位弹药;
单位范围;
单位功率;
团队;
字符类型字符类型;
公众:
角色(单位生命值、单位弹药、单位射程、单位力量、,
团队,角色类型)
:生命(生命)、弹药(弹药)、射程(射程)、力量(力量),
团队(团队),角色类型(类型){
虚拟字符*clone()常量=0;
字符(常量字符和字符)=默认值;
};
}//名称空间mtm
#恩迪夫
#ifndef MTM_狙击手_
#定义MTM_狙击手_
//#包括“Character.h”
#包括
名称空间mtm{
职业狙击手:公众人物{
私人:
bool empty=false;
单位攻击成功=0;
公众:
狙击手(单位生命值、单位弹药、单位射程、单位力量、,
团队,CharacterType类型,bool empty=false,
单位\u t成功\u攻击=0)
:角色(生命值、弹药、射程、力量、团队、类型),
空(空),成功的\u攻击(成功的\u攻击){}
虚拟角色*clone()常量重写{返回新狙击手(*this);}
狙击手(常数狙击手和狙击手)=默认值;
};
}//名称空间mtm
#恩迪夫
int main(){
std::唯一的\u ptr ch;
ch=std::make_unique(1,1,1,1,Team::team1,CharacterType::hulk);
}
看起来您可能正在实例化一个抽象类(因为virtual方法是纯virtual(=0))
确保您正在动态实例化虚拟类型,而不是使用newcharacter()
——因为这会命名抽象类型
所以不要:
Character x(.....);
也不要:
Character x = Sniper(....); // slicing to abstract base
注:
- 您不在构造函数中初始化
团队
,而且为空
也是未使用的
工作样本:
using units_t = int;
enum class Team {team1};
enum class CharacterType {hulk};
#ifndef MTM_CHARACTER_H_
#define MTM_CHARACTER_H_
//#include "Exceptions.h"
//#include "Auxiliaries.h"
#include <memory>
namespace mtm {
class Character {
protected:
units_t health;
units_t ammo;
units_t range;
units_t power;
Team team;
CharacterType character_type;
public:
Character(units_t health, units_t ammo, units_t range, units_t power,
Team team, CharacterType type)
: health(health), ammo(ammo), range(range), power(power),
team(team), character_type(type) {}
virtual Character* clone() const = 0;
Character(const Character& character) = default;
};
} // namespace mtm
#endif
#ifndef MTM_SNIPER_H_
#define MTM_SNIPER_H_
//#include "Character.h"
#include <memory>
namespace mtm {
class Sniper : public Character {
private:
bool empty = false;
units_t successful_attack = 0;
public:
Sniper(units_t health, units_t ammo, units_t range, units_t power,
Team team, CharacterType type, bool empty = false,
units_t successful_attack = 0)
: Character(health, ammo, range, power, team, type),
empty(empty), successful_attack(successful_attack) {}
virtual Character* clone() const override { return new Sniper(*this); }
Sniper(const Sniper& sniper) = default;
};
} // namespace mtm
#endif
int main() {
std::unique_ptr<mtm::Character> ch;
ch = std::make_unique<mtm::Sniper>(1,1,1,1,Team::team1,CharacterType::hulk);
}
使用单位\u t=int;
枚举类团队{team1};
枚举类CharacterType{hulk};
#ifndef MTM_字符_
#定义MTM_字符_
//#包括“Exceptions.h”
//#包括“附件h”
#包括
名称空间mtm{
类字符{
受保护的:
单位卫生;
单位弹药;
单位范围;
单位功率;
团队;
字符类型字符类型;
公众:
角色(单位生命值、单位弹药、单位射程、单位力量、,
团队,角色类型)
:生命(生命)、弹药(弹药)、射程(射程)、力量(力量),
团队(团队),角色类型(类型){
虚拟字符*clone()常量=0;
字符(常量字符和字符)=默认值;
};
}//名称空间mtm
#恩迪夫
#ifndef MTM_狙击手_
#定义MTM_狙击手_
//#包括“Character.h”
#包括
名称空间mtm{
职业狙击手:公众人物{
私人:
bool empty=false;
单位攻击成功=0;
公众:
狙击手(单位生命值、单位弹药、单位射程、单位力量、,
团队,CharacterType类型,bool empty=false,
单位\u t成功\u攻击=0)
:角色(生命值、弹药、射程、力量、团队、类型),
空(空),成功的\u攻击(成功的\u攻击){}
虚拟角色*clone()常量重写{返回新狙击手(*this);}
狙击手(常数狙击手和狙击手)=默认值;
};
}//名称空间mtm
#恩迪夫
int main(){
std::唯一的\u ptr ch;
ch=std::make_unique(1,1,1,1,Team::team1,CharacterType::hulk);
}
就像你昨天(在我的时区)发布类似代码一样,你没有提供足够的信息。阅读如何提供-这是一个小但完整的代码示例,其他人可以使用它来重现您看到的症状。没有这些,你就是在强迫人们猜测。我的猜测是,由于链接器(不是编译器)没有找到与Character
类相关的关键输入,您在链接时遗漏了一个对象文件(可能是Character.o
)。此问题显示的代码不符合stackoverflow.com对。这意味着这里的任何人都不可能最终回答这个问题;但最多只能猜测。你的问题应该显示一个最小的例子,不超过一到两页的代码(“最小”部分),其他人可以剪切/粘贴、编译、运行和复制所描述的问题(“可复制”部分),完全如图所示(这包括任何辅助信息,如程序输入)。有关详细信息,请参阅。@Peter我编辑了我的question@Peter我不能留下一个字符的对象文件,我正在使用这个命令行,g++-g-std=c++11-Wall-Werror-pedantic errors-DNDEB