I cannot seem to access the map in the parent class from the child class, as I try to output the contents of the map and nothing is displayed.
BELOW I HAVE ADDED IN THE FULL CODE, OVERALL IF I POPULATE THE MAP IN THE transLateMask() it will then print out the contents, however if i populate the map in the populate() it will print out nothing and I do not know why?
Here is my code.
//HEADER FILE
//////PARENT CLASS
#include <iostream>
#include <string>
struct TTYElementBase
{
//some code here
};
class element
{
public:
std::map<char,std::string> transMask;
std::map<char,std::string>::iterator it;
void populate();
};
//////CHILD CLASS .HPP
class elementV : public element
{
public :
std::string s1;
std::string s2;
elementV();
friend ostream &operator<< (ostream &, const elementV &);
void transLateMask();
};
//CPP FILE #include "example.h"
elementV::elementV()
{
}
void element::populate()
{
transMask['D']='D'; //WILL PRINT OUT NOTHING IF I POPULATE HERE
}
void elementV::transLateMask()
{
for ( it=transMask.begin() ; it != transMask.end(); it++ )
std::cout << (*it).first << std::endl;
}
int main()
{
element e;
e.populate();
elementV v;
v.transLateMask();
}
It doesn’t output anything – why is that?
The call to e.populate() populates e.transMask, not v.transMask. You need to call v.populate().