2

I am unable to use add values this map defined in header file as protected attribute from class member function.

class xyz{
protected:
 map < string, string > *tagsMap;
public:
 xyz();
 //set Tags
 void addTag(string _tagName, string _tagValue) const;
}
// In cpp class, 
//set new Tags
void xyz::addTag(string _tagName, string _tagValue) const {
 //map < string, string > tagsMap ;
 //gives error until I uncomment above line, but thats local map
 tagsMap.insert(pair<string, string>(_tagName, _tagValue));
 // first insert function version (single parameter):
 tagsMap .insert(pair<string, string>(_tagName, _tagValue));
 for (auto& t : tagsMap )
 cout << "addTag():" << t.first << "=" << t.second << endl;
}
Paul R
214k38 gold badges402 silver badges578 bronze badges
asked Jun 1, 2018 at 8:36
3
  • 1
    Why do you need a pointer to a map? Commented Jun 1, 2018 at 8:43
  • i dont need a pointer.. i just want to save new values to the map on every function call and later access them... Commented Jun 1, 2018 at 8:44
  • 1
    also add ; after your class def Commented Jun 1, 2018 at 8:54

2 Answers 2

2

You have 3 problems:

1) Class member is declared a pointer

The commented line inside addTag():

// map < string, string > tagsMap;

It is not a pointer, which is the reason why it works if you uncomment the local map declaration.

However, this is not correct logically since it's not a member of your class - it shadows your tagsMap class member.

Thus, you need to declare tagsMap in your xyz class a non-pointer.

map < string, string > *tagsMap;
 // ^ remove asterisk '*'

2) Missing Semicolon after class definition

Add ; semicolon, after your class definition

class xyz {
 ...
}
// ^^^ add semicolon here

3) Const function

Remove const in addTag() to be able to write on tagsMap class member

void xyz::addTag(string _tagName, string _tagValue) const { .. }
 // ^^^^^ remove const
void addTag(string _tagName, string _tagValue) const;
 // ^^^^^ remove const
answered Jun 1, 2018 at 8:44

1 Comment

And if you do need to use a pointer for some reason, you need to use '->' to call member functions rather than just '.'
2

Yes pointer was not needed. It worked after making the function non-const as suggested by @codekaizer in comments above.

class xyz{
protected:
map < string, string > tagsMap;
public:
xyz();
//set Tags
void addTag(string _tagName, string _tagValue);
}

// In cpp class,

void xyz::addTag(string _tagName, string _tagValue) {
 tagsMap.insert(pair<string, string>(_tagName, _tagValue));
}
answered Jun 1, 2018 at 9:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.