2

I have a player variable which contains a vector of Resource class which derived from Name and ID class.

Problem lies when I'm compiling the code and the following errors appear during compilation.

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' 

Basically what I did in Resources.cpp is, I had a function that initializes the Resources of the player's vector. I was weirded out by this error because I did not I looked around in StackOverflow for some answers but I haven't seen some helpful answers. This link came close but I'm not sure if changing the const into a non const value is what I need.

Here's the code.

in 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 } } 

in 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 

in IdClass.cpp

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

This is a small piece of the code I'm working on. If you guys need more explanation or code, I can give some in the comments below.


Edit 1: Looks like I forgot to place the error code after the output. Also I'm using 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 

Changing the variable from const short to short in IdClass.h would create a linker error in one of my classes.

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 

So obviously the two threads that two commenters linked weren't that helpful.

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

5
  • 1
    I can only see 'notes', not errors. Does it build an executable, or are there errors that you're not showing? The note is basically saying 'can't make a default assignment operator, because of the const member'. Commented Jul 24, 2017 at 10:04
  • you want to know how to copy an object when it has const members? Commented Jul 24, 2017 at 10:06
  • 1
    read the accepted answer here: stackoverflow.com/questions/634662/… It explains why you cannot use '=' on objects that have const members . If the project builds correctly, and you do not assign 2 objects of type resources, you should be OK Commented Jul 24, 2017 at 10:08
  • Possible duplicate of Non-static const member, can't use default assignment operator Commented Jul 24, 2017 at 10:09
  • updated the post Commented Jul 24, 2017 at 11:01

2 Answers 2

1

You solved your first problem by changing const short to short. This was your last compilation error , so you moved to the linking stage. This exposed your second (unrelated!) error, a static string playerName in class FileManager which has a declaration , but no definition.

In c++ it is not enough to "declare" a static such as

class A { static int a; } 

you also need to "define" it, or give it a body by adding the following line in some .cpp file ( not .h as it may get re-defined multiple times )

int A::a = 0; 

However, I'm not sure you really want playerName to be a static member in class FileManager. This would mean many file managers would all share the same player name.

Sign up to request clarification or add additional context in comments.

Comments

1

Your IdClass class is not copyable/movable because of const member

 const short id; 

As the result your Resources class becomes not copyable/moveable, but it is needed by std::vector.

Remove const from short id to make it compiles.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.