In my header file, I have
typedef map <string, MyClass*> myMap;
class MainClass {
myMap map;
public:
friend istream& operator>> (istream &is, MainClass &mainc) {
string name = "Geo";
MyClass* sample = new MyClass();
map.insert(myMap::value_type(name, sample) );
return is; }
};
During compilation, I get:
line 4: error: invalid use of non-static data member 'MainClass::map'
line 9: error: from this location
I have tried changing myMap map to myMap mapa but I’m getting the same error.
Since your
operator>>is a friend toMainClass, it’s not associated with a specific instance ofMainClass(i.e., it doesn’t receive athispointer).Therefore, when you try to do:
The compiler doesn’t know what instance’s
mapmember you want to refer to. Clearly, in this case, you mean:…since
maincis the instance ofMainClasswhose reference you received as the destination for the data you read.