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.
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;
}
Usage (returning value)... You? Just ignore it? (means the function returns nothing)
If so, remove the returning value then replace it with word 'void'.
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
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;
}
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
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/
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, ...
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;
}
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
@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.
@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))...