How to access map outside the class

gauravkr 1 Tallied Votes 183 Views Share

#ifndef _READFILE_H_
#define _READFILE_H_
#include<string>
#include<vector>
#include<map>

using namespace std;

class Readfile{

private:
string str;
vector <string> goArray;
vector <string> strArray;
vector <string> phyIntArray;
vector <string> subArray;
vector <string> proArray;
/* member function */
void processString(string str);
void processGoId(string);
void processPhysicalInt(string);
// void processMetabolicInt(string);

public:
typedef struct Info{
string uniprotID;
vector <string> goId;
vector <string> phyInt;
};

map <int,struct Info> proteinData;
map <int,struct Info>::iterator it;

Readfile(string file);//constructor

};
#endif

Hi All,

I'm new to C++. I need some help regarding the usage of map STL container. I've declare map proteinData in the below header file. ProteinData contains key as integer and value as a struct. 
Can any one help me how to access map outside the class defination.

Thanks in advance
gauravkr 0 Newbie Poster

I'm accessing map proteinData inside the
ini main(){
Readfile *myfile=new Readfile(argv[1]);
cout << myfile->proteinData << endl;
}

I'm getting an error:
no match for ‘operator<<’ in ‘std::cout << myfile->Readfile::proteinData’.

i'm clueless about the error.

pecet 1 Junior Poster in Training

You have to define your own operator<<

gauravkr 0 Newbie Poster

'm able to get the size of the map container proteinData, shown in the below code.

Readfile *myfile=new Readfile('foo.txt');
cout << "this is iterator:" << endl;
cout << myfile->proteinData.size()<< endl;

for(myfile->it=myfile->proteinData.begin(); myfile->it=myfile->proteinData.end();myfile->it++){
cout << myfile->it->first << endl;
}

but couldn't access the iterator defined inside the class, giving an error message

could not convert ‘(myfile->Readfile::it <unknown operator> ((const std::_Rb_tree_iterator<std::pair<const int, Readfile::Info> >&)((const std::_Rb_tree_iterator<std::pair<const int, Readfile::Info> >*)(& myfile->Readfile::proteinData.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = int, _Tp = Readfile::Info, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Readfile::Info> >]()))))’ to ‘bool’.

I think there is something wrong with defining the iterator inside class.

Can anyone help with this.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.