C++ 基类调用已删除或无法访问的函数



我有一个player变量,其中包含一个Resource类的向量,该向量派生自NameID类。

问题在于我编译代码时,编译过程中出现以下错误。

resources.h(27): note: 'Resources &Resources::operator =(const Resources &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function  'IdClass &IdClass::operator =(const IdClass &)'
idclass.h(11): note: 'IdClass &IdClass::operator =(const IdClass &)': function was implicitly deleted because 'IdClass' has a data member 'IdClass::id' of const-qualified non-class type
idclass.h(4): note: see declaration of 'IdClass::id'

基本上我在Resources.cpp中所做的是,我有一个初始化玩家向量资源的函数。我对这个错误感到奇怪,因为我没有在 StackOverflow 中寻找一些答案,但我还没有看到一些有用的答案。这个链接很接近,但我不确定是否需要将常量更改为非常量值。

这是代码。

在 资源.h 中

#include "NameClass.h"
#include "IdClass.h"
#include "settings.h"
class PlayerClass;
class Resources : public NameClass, public IdClass
{
public:
/*Sets the name and id*/
Resources(string n, const short identifier):NameClass(n), IdClass(identifier){}
static void InitializeResourceContainer(PlayerClass& player){
// playerInv is a struct
// rssContainer is a vector<Resources>
// name_x and id_x comes from settings.h which is #defined
player.playerInv.rssContainer.push_back(Resources(name_Stone, id_Stone));
player.playerInv.rssContainer.push_back(Resources(name_Wood, id_Wood));
player.playerInv.rssContainer.push_back(Resources(name_Plastic, id_Plastic));
player.playerInv.rssContainer.push_back(Resources(name_Thatch, id_Thatch));
player.playerInv.rssContainer.push_back(Resources(name_Fabric, id_Fabric)); // this is line 27
// plus a lot of resources
}
}

在 IdClass.h 中

class IdClass
{
const short id;// line 4
public:
/*Sets the id*/
IdClass(const short idToSet);
/*returns the id*/
short GetId() const;
}; // line 11

在 IdClass 中.cpp

#include "IdClass.h"
/*Sets the id*/
IdClass::IdClass(const short idToSet) : id(idToSet)
{
}
/*returns the id*/
short IdClass::GetId() const { return id; }

这是我正在处理的一小段代码。如果你们需要更多解释或代码,我可以在下面的评论中给出一些。


编辑 1: 看起来我忘了在输出后放置错误代码。我也在使用Visual Studio 2017

Error C2280 'Resources &Resources::operator =(const Resources &)': attempting to reference a deleted function f:visual studio2017vctoolsmsvc14.10.25017includexutility  2310

在 IdClass.h 中将变量从const short更改为short会在我的一个类中创建链接器错误。

Error LNK1120   1 unresolved externals  
Error LNK2001   unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > FileManager::playerName" (?playerName@FileManager@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)    ArchizzleGame   C:UsersCraftedGamingDocumentsVisual Studio 2017ProjectsArchizzleGameArchizzleGameFileManager.obj    1

所以很明显,两个评论者链接的两个线程并没有那么有帮助。

我的目标是将 id 值从 Resources.h 获取到 IdClass.h。

您通过将常量短更改为短来解决第一个问题。 这是你最后的编译错误,所以你进入了链接阶段。这暴露了你的第二个(不相关的!(错误,一个静态字符串播放器在类FileManager中有一个声明,但没有定义。

在 c++ 中,仅仅"声明"静态(例如

class A
{
static int a;
}

您还需要"定义"它,或者通过在某个.cpp文件中添加以下行来给它一个主体(不是 .h,因为它可能会多次重新定义(

int A::a = 0;

但是,我不确定您是否真的希望 playerName 成为类文件管理器中的静态成员。这意味着许多文件管理器将共享相同的玩家名称。

由于常量成员,您的 IdClass 类不可复制/移动

const short id;

结果,您的 Resources 类变得不可复制/可移动,但 std::vector 需要它。

short id中删除 const 以使其编译。

最新更新