David Sttivend Angel
David Sttivend Angel

Reputation: 123

how to print a Map Object?

sorry if my question is to newbish, but i cant find a solution.

i have a class named Transition that have a map called inNext, and i want to print this Transition object, but i get an error about "cant find the begin or end members" (from map class)

class Transition{

public:
    Transition():inNext(){};
    ~Transition(){};

    map<string, string>& getTransition(){
        return inNext;
    }

    void setTransition(string a, string b){

        inNext.insert(pair<string,string>(a,b));
    }

    void printTransition(Transition a){
        map <string, string>::iterator it;
        for(it = a.begin(); it != a.end(); it++){

            cout << (*it).first << ","<<(*it).second << endl;
        }

    }


private:
    map<string, string> inNext;

};

Upvotes: 1

Views: 1521

Answers (2)

Daniel Frey
Daniel Frey

Reputation: 56863

Your method is weird: It's a member function and it takes another instance of Transition (and even copies it for no reason) as an argument. You probably want

void print() {
    // you want to print the content of the map inNext:
    for(map <string, string>::iterator it = inNext.begin(); it != inNext.end(); it++) {
        cout << it->first << "," << it->second << endl;
    }
}

which is called like this:

Transition myTransition = ...;

myTransition.print();

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

begin and end are members of std::map not of your class. You need to call a.inNext.begin() and a.inNext.end().

Upvotes: 0

Related Questions