I have a map member of a class.
Inside a function of the class, I call nameOfMap.insert(), and the compiler gives an error saying:
error: expected unqualified-id before '.' token
I have a map member of a class.
Inside a function of the class, I call nameOfMap.insert(), and the compiler gives an error saying:
error: expected unqualified-id before '.' token
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Code:#include <map> #include <vector> #include <string> using namespace std; class CaClass { public: CaClass(); //constructor void doStuff(); private: typedef map<unsigned int, string> aMap; vector<string> aStringVector; vector<unsigned int> aUnsignedIntVector; }; CaClass::CaClass() { //nothing in here that deals with the map.... } void CaClass::doStuff() { unsigned int currentNum = aUnsignedIntVector.at(0); unsigned int i = 0; do { aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1))); //note that I'm using currentNum - 1 here, since every element of the unsigned int vector is 1 more than the corresponding element in the string vector currentNum = aUnsignedIntVector.at(i+1); i++; while(i < aUnsignedIntVector.size()); }
Last edited by Programmer_P; 06-18-2010 at 10:05 AM.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Where are the template arguments for map?
std::map<Key, Value>
Also, you could easily write this......as this:Code:aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));Code:aMap[currentNum - 1] = aStringVector.at(currentNum - 1);
Yeah, I noticed that before you posted.
My bad. I already fixed it in my last post.
I meant to make the map declaration in the class look like this:
Because, that's how it is in the real code.Code:typedef map<unsigned int, string> aMap;
Thanks, I will try that.Also, you could easily write this......as this:Code:aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));Code:aMap[currentNum - 1] = aStringVector.at(currentNum - 1);
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
O_oCode:aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));
According to the code, `aMap' is a type; it is not an instance. Is that really your code?
Soma
Ok, using your example changed the error message to this instead:
error: expected unqualified-id before '[' token
Yes, because I'm copying the style of someone else (actually 'Bubba' on these forums) when writing a map because I don't know much about maps. I originally tried the code with the map declaration looking like this instead:
But then the compiler printed out a message saying something like the map wasn't a class type, so I changed it to use a typedef. Anyway, I just changed it back to that after reading your post, and now its getting past that error, so it appears that it is solved.Code:map <unsigned int, string> aMap;
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Well, apparently it did matter using the typedef in this case, since once I changed the insert calls in my code to using the '[]' and '=' operators instead, like in your example, the compiler still gave an error message on the map line, when I was still using typedef. Then, following phantom's post, I removed the 'typedef' before the map declaration inside the class, and now the compiler doesn't give me any error on the same line of code adding stuff to the map.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Maybe. I've been wondering if I need to call the constructor of the map object inside my class constructor first before I try calling a member function of map.
For some reason, though, I thought an object of map would automatically be created by the compiler when you use the '.' operator to access a member function of my member map.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Well here's the problem.
aMap is either a type, in which stuff like this is valid:
aMap::value_type (foo);
Or it is an instance, which lets you insert things:
aMap[bar] = foo;
It cannot be both.
And yet it would not let me use the insert() function to insert things into the map, when it was just an instance and not a typedef. It was only when I changed it to use the '[]' and '=' operators, like in your example, that it got past the compiler error. Why is that?
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.