C++ c++;基类调用已删除或不可访问的函数

C++ c++;基类调用已删除或不可访问的函数,c++,compiler-errors,constants,deleted-functions,C++,Compiler Errors,Constants,Deleted Functions,我有一个player变量,它包含一个Resource类的向量,该向量来自Name和ID类 问题在于我在编译代码时,在编译过程中会出现以下错误 resources.h(27): note: 'Resources &Resources::operator =(const Resources &)': function was implicitly deleted because a base class invokes a deleted or inaccessible functi

我有一个
player
变量,它包含一个
Resource
类的向量,该向量来自
Name
ID

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

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中四处寻找一些答案,但我没有看到一些有用的答案。这很接近,但我不确定将常量值更改为非常量值是否是我所需要的

这是密码

在资源中

#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.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 studio\2017\vc\tools\msvc\14.10.25017\include\xutility  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:\Users\CraftedGaming\Documents\Visual Studio 2017\Projects\ArchizzleGame\ArchizzleGame\FileManager.obj    1
错误LNK1120 1未解析的外部
错误LNK2001未解析的外部符号“private:static class std::basic_string FileManager::playerName”(?)?playerName@FileManager@@0V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)存档游戏C:\Users\CraftedGaming\Documents\Visual Studio 2017\Projects\ArchizzleGame\ArchizzleGame\FileManager.obj 1
所以很明显,两位评论者链接的两条线索没有那么大的帮助

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


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

  const short id;
因此,资源类变得不可复制/移动,但std::vector需要它


short id
中删除常量,使其编译

通过将const short改为short,您解决了第一个问题。这是您最后一次编译错误,因此您已转到链接阶段。这暴露了您的第二个(不相关!)错误,即类FileManager中的静态字符串playerName,它有一个声明,但没有定义

在C++中,“声明”静态如

是不够的
class A
{
 static int a;
}
您还需要“定义”它,或者通过在某个.cpp文件中添加以下行(而不是.h,因为它可能会被多次重新定义)为它提供一个主体


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

我只能看到“注释”,而不能看到错误。它是否构建了一个可执行文件,或者是否存在您没有显示的错误?注释基本上是说“由于常量成员,无法创建默认赋值运算符”。您想知道当对象具有
const
成员时如何复制该对象吗?请阅读此处接受的答案:它解释了为什么不能在具有常量成员的对象上使用“=”。如果项目生成正确,并且您没有分配两个resources类型的对象,那么您应该知道更新后的文章可能是重复的
class A
{
 static int a;
}
int A::a = 0;