printing values from multimap

Hello again,

This little bug has been plaguing me for some time...

I have a self defined type Derivation. The operator << has been overloaded and working elsewhere in the program.

Now I have this piece of code
1
2
3
4
5
6
7
8
void ProductionRuleContainer::addProduction(std::string NT, std::string deriv)
{
    Derivation d;
    d.addDerivString(deriv);
    std::cout << "Entering Production Rule:" << d << std::endl;
    prcontainer.insert(std::pair<std::string, Derivation>(NT, d));
    std::cout << NT << ": "<< prcontainer.count(NT) <<  std::endl;
}


This performs exactly the way I want it. It shows the output of d the way I want, before entering in the multimap and the counter works perfectly. The prcontainer multimap is defined as followed:

 
std::multimap<std::string, Derivation> prcontainer;


Now comes the trouble, I try printing the multimap using the following code:

1
2
3
4
5
6
7
8
9
10
void ProductionRuleContainer::print() {
    std::cout << ".PRULE" << "\n";
    std::multimap<std::string, Derivation>::iterator it;
    for (it = prcontainer.begin(); it != prcontainer.end();
	  ++it)
	   {
	       std::cout << (*it).first << "\t::==\t" << (*it).second  << std::endl;
	   }
    std::cout << ".END_PRULE" << "\n";
}


It prints the key no problem, which is just a string. But it doesn't print the corresponding derivation value, of which << is loaded and working elsewhere. What am I doing wrong?
Do I need to add a comparer or something for the derivation?
If so how and where must I define this...

Thank you in advance
Can you post the error message?

You could also try modifying it slightly like this to narrow down the error?

1
2
3
4
5
6
7
8
9
10
11
void ProductionRuleContainer::print() {
    std::cout << ".PRULE" << "\n";
    std::multimap<std::string, Derivation>::iterator it;
    for (it = prcontainer.begin(); it != prcontainer.end();
	  ++it)
	   {
	       Derivation& d = (*it).second;
	       std::cout << (*it).first << "\t::==\t" << d  << std::endl;
	   }
    std::cout << ".END_PRULE" << "\n";
}
Also can you show us how you defined the operator<< for the Derivation class?
hello,

I have adjusted the code and it produces the same results.
there is no error message, it just doesn't print what I want...
The key is printed (a string) and then the tabs and "::==" but It will not print the derivation.

the derivation operator << is really very simple.
1
2
3
4
std::ostream & operator<<(std::ostream& output, const Derivation& data) {
    return output << data.getOString();
}


This is the header file for derivation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Derivation {
public:
    Derivation();
    Derivation(const Derivation& orig);
    virtual ~Derivation();
    friend std::ostream & operator<<(std::ostream&, const Derivation&);
    void addElement(std::string x);
    void addDerivString(std::string x);
    std::string getElementId(int entryNo);

private:

    std::string ostring;
    const std::string& getOString() const {
        return ostring;
    }


};


As you see << is no more than printing the ostring.

On line 5, of the first post (addProduction), I am able to print d using << , without problems.
When calling print which invokes << from a derivation iterating through the multimap, it doesn't work.
As I can print the derivation correctly before adding inclines me to believe that the problem occurs when adding to or even when invoking the multimap in the first place.

thank you
Last edited on
Does the code in your copy constructor copy the string 'ostring' over?
You could also test if the Derivation object was not copied correctly by checking it immediately after insertion:

1
2
3
4
5
6
7
8
9
10
void ProductionRuleContainer::addProduction(std::string NT, std::string deriv)
{
    Derivation d;
    d.addDerivString(deriv);
    std::cout << "Entering Production Rule:" << d << std::endl;
    std::multimap<std::string, Derivation>::iterator it = prcontainer.insert(std::pair<std::string, Derivation>(NT, d));
    std::cout << "After insertion: " << *it << std::endl; // check if it was coppied in okay

    std::cout << NT << ": "<< prcontainer.count(NT) <<  std::endl;
}
Last edited on
Yes, problem solved!

Thank you!
Topic archived. No new replies allowed.