enum to string

Pages: 12
hey guys.
i tried to get a string output for an enum. this is what i have so far, but it's not working. maybe you can help me with that or have a better idea.

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
30
31
32
33
enum COLOR
{
	RED,
	YELLOW,
	GREEN,
	CYAN,
	BLUE,
	VIOLET 
};


class Detection
{
public:
	Detection(void);
	~Detection(void);
        /*some functions*/
};

std::ostream &operator << (std::ostream &os, const COLOR &color)
{
	switch(color)
	{
		case RED: os << "red"; break;
		case YELLOW: os << "yellow"; break;
		case GREEN: os << "green"; break;
		case CYAN: os << "cyan"; break;
		case BLUE: os << "blue"; break;
		case VIOLET: os << "violet"; break;
	}
	return os;
}

What is this? I don't understand.
return os;

Can you explain more your problem please?
without line 31 "return os;" i got the error: 'operator<<': Has to return a value

so i added return os; to return the outputstream, but now i get a linker error
Why your function returns an ofstream value? Name? Usage?
i want to write std::cout << RED in my class.cpp file
Usage (returning value)... You? Just ignore it? (means the function returns nothing)
If so, remove the returning value then replace it with word 'void'.
i dont know what you mean

i got the idea for the code i posted from: http://msdn.microsoft.com/en-us/library/1z2f6c2k%28v=vs.80%29.aspx
but it's not working
This should work.

what is the linker error? multiple definitions? you need to place the prototype in the header file and the function itself in the implementation file (.cpp) just like any other function. or alternatively if you want make the function inline
Sorry my screen is limited.
But your function...(looks weird). You want it to return a colour, or anything else?
i forgot lines 18,19. now i get the error:
binary operator '<<' has too many parameters

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
30
31
32
33
34
enum COLOR
{
	RED,
	YELLOW,
	GREEN,
	CYAN,
	BLUE,
	VIOLET 
};


class Detection
{
public:
	Detection(void);
	~Detection(void);
        /*some functions*/
private:
       std::ostream & operator<<(std::ostream &os, const COLOR &color);
};

std::ostream &operator << (std::ostream &os, const COLOR &color)
{
	switch(color)
	{
		case RED: os << "red"; break;
		case YELLOW: os << "yellow"; break;
		case GREEN: os << "green"; break;
		case CYAN: os << "cyan"; break;
		case BLUE: os << "blue"; break;
		case VIOLET: os << "violet"; break;
	}
	return os;
}
An operator only can hold at least and at most (1 parameter)
Last edited on
the msdn docu also uses 2 parameters

ostream& operator<<(ostream& os, const Date& dt);



basically i'm just looking for a way to use the enum for a string output, without using if statements all the time. so if you know any solution to this, please post it here
Last edited on
that's only if it's declared as a member function of a class. In this case, it has been declared as a global function, so it needs 2 parameters. http://www.cplusplus.com/doc/tutorial/classes2/
But, Why do you still keep ostream?
You want :
std::cout..
right?
And although a operator may support multiple parameters, but do you know how to use this? - Redundant
no, std::cout is an ostream object which is specifically for the standard output. (a) you can't return it because it's not a type and (b) by using ostream&, you can write to anywhere - ofstream, ostringstream, ostream, ...
and, in general, you use global operators as explained by the table at the bottom of 'overloading operators': http://www.cplusplus.com/doc/tutorial/classes2/
ok it works now. coder777 was right, that's what i use now

in the Header, outside the class:
std::ostream &operator<<(std::ostream &os, const COLOR &color);

in the class cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::ostream &operator<<(std::ostream &os, const COLOR &color)
{
	switch(color)
	{
		case RED: os << "red"; break;
		case YELLOW: os << "yellow"; break;
		case GREEN: os << "green"; break;
		case CYAN: os << "cyan"; break;
		case BLUE: os << "blue"; break;
		case VIOLET: os << "violet"; break;
	}
	return os;
}
Last edited on
you might want the following file structure:

color.hpp:
1
2
3
4
5
6
7
8
9
10
enum COLOR
{
	RED,
	YELLOW,
	GREEN,
	CYAN,
	BLUE,
	VIOLET 
};
std::ostream &operator << (std::ostream &os, const COLOR &color);


color.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::ostream &operator << (std::ostream &os, const COLOR &color)
{
	switch(color)
	{
		case RED: os << "red"; break;
		case YELLOW: os << "yellow"; break;
		case GREEN: os << "green"; break;
		case CYAN: os << "cyan"; break;
		case BLUE: os << "blue"; break;
		case VIOLET: os << "violet"; break;
	}
	return os;
}


detection.hpp
1
2
3
4
5
6
7
class Detection
{
public:
	Detection(void);
	~Detection(void);
        /*some functions*/
};


@Jackson Marie
hand out advice only when you know what you're talking about. I.e. stop trolling
1
2
@Jackson Marie
hand out advice only when you know what you're talking about. I.e. stop trolling  


Wasted effort, I'm afraid. He does this all the time. Occasionally, he gives good advice, but most of the time he talks nonsense and gives the worst possible "help".

Unfortunately, he also has far too much spare time, and seems to love spending it posting here.
Last edited on
@MikeyBoy (58)

@Jackson Marie
hand out advice only when you know what you're talking about. I.e. stop trolling



Wasted effort, I'm afraid. He does this all the time. Occasionally, he gives good advice, but most of the time he talks nonsense and gives the worst possible "help".

Unfortunately, he also has far too much spare time, and seems to love spending it posting here.


I'm very sorry, but I really need more time to correct myself. Don't just call me "troll" because of many many things... (Actually I'm between Beginner - me - knowledgeable (experienced))...

That's my biggest problem. and
love spending it posting here.

Just answer "No". Because I'm not an idiot.

Pages: 12