eclipse noob

Hey,

this is my first post so thanks to anyone that can help

My problem is I keep getting the error make::***[source/P3v2.o]Error 1 @ line 0

This is my code and I assume their is something wrong with it.

I am trying to use a set within a map and not sure if I am putting the data in properly is that it?

Many Thanks





#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>

using namespace std;

std::map<string,set<string> > token;

int main()
{

token["s1"].insert("3");

cout<< token["s1"]<<endl;


return 0;
}
First the syntax is:
 
token["s1"] = s1;

Second, you are trying to insert a string into your map but the map requires a set<string>
1
2
3
set<string> s1;
s1.insert("3");
token["s1"] = s1;

...should do it.
Last edited on
Hey Galik thanks for the reply.

I did what you said and I still get the error:

make:***[source/P3v2.o]Error 1 @ line 0


Update!!!!!

Hey I know the problem is to do with the output statement as when I comment this out it works fine.

Thanks
Last edited on
The original posted code is OK - EXCEPT for the cout statement.

The following should work (unless he has some real issue with his eclipse setup)

He needs to write a global << overload function in order to do cout on a set<string>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>

using namespace std;

std::map<string,set<string> > token;

ostream& operator << (ostream & os, set<string> & ss)
{
    set<string>::iterator it;
    for (it =ss.begin(); it != ss.end(); ++it )
    {
        cout << *it << '\n';
    }
    return os;
}

int main()
{

    token["s1"].insert("3");

    cout<< token["s1"]<<endl;

    return 0;
}
Last edited on
Thanks gulkan that now works perfectly,

However don't really understand why it works? could you breifly explain the reason

Many thanks again
See, your operator << was never meant to output std::sets like that. There was no version of it that could take an std::ostream& on one side and an std::set<string> on the other, and return an std::ostream&. So, what gulkan did was defined such an operator, or rather, he overloaded the existing operator, making it change meanings for different types. He still uses the same << symbol, but it has a different meaning when what's on the left is an std::ostream& and what's on the right is an std::set<string>.

Line 11 declares the operator overload, which is similar in syntax and usage to a function declaration, and everything from there up to line 19 explains what it does.

Here's a template for future usage:
/*returntype*/ operator /*symbol*/ (/*left-argument*/, /*right-argument*/)
Note that there are only a finite number of symbols that can be overloaded like this.

I hope this helps!

-Albatross
Last edited on
great post albatross, you did a really great job explaining that!
Yer albatross thanks for that explanation
Topic archived. No new replies allowed.