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

标签 c++ compiler-errors constants deleted-functions

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

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

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 中所做的是,我有一个函数可以初始化玩家 vector 的资源。我对这个错误感到很奇怪,因为我没有在 StackOverflow 中寻找一些答案,但我没有看到一些有用的答案。这个link很接近,但我不确定将 const 更改为非常量值是否是我所需要的。

这是代码。

在Resources.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 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

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

What I aim to do is to get the id value from Resources.h to IdClass.h.

最佳答案

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

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

)是不够的
class A
{
 static int a;
}

您还需要“定义”它,或者通过在某些 .cpp 文件中添加以下行来为其提供主体(不是 .h,因为它可能会被重新定义多次)

int A::a = 0;

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

关于C++ 基类调用已删除或无法访问的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277603/

相关文章:

C++ struct - 作为此参数传递 const 会丢弃限定符

c - ncurses库编译错误

c# - WebSockets 我需要什么 'using' 指令

c++ - const char * 允许改变字符值,为什么?

C++20 在三元语句中返回元组

C++ Lambda 段错误

c++ - 如何让g++运算符重载错误更简洁?

c# - 如何在类中包含 "Empty"常量

c++ - 如何将 char 数组定义为常量?

c++ - 在 C++ 中使用函数对数组进行排序