我有一个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 中四处寻找一些答案,但我没有看到一些有用的答案。这个链接很接近,但我不确定将 const 更改为非 const 值是否是我需要的。
这是代码。
在资源.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
所以很明显,两个评论者链接的两个线程并没有那么有帮助。
我的目标是从 Resources.h 获取 id 值到 IdClass.h。