I am facing a problem in c++:
#include <iostream>
class Apple{
public:
int price = 100;
enum class color {
red =1, green, yellow
};
};
int main() {
Apple apple;
std::cout << Apple::color::green << std::endl;
}
When I try to compile this code following message appears:
[Error] 'Apple::color' is not a class or namespace
After using correct c++11 flag, you will have to overload
operator <<
P0W's answer is correct on both counts, but in case you simply want to output the underlying value, then it may be simpler to cast rather than overload the insertion operator.
In order to use
enum class
your compiler has to support C++11. This can be for example achieved by appending a-std=c++11
after your clang or g++ build command if you are using those. New versions of Visual Studio enable C++11 automatically.The error that you should be getting is
no operator "<<
, as pointed out by @pvl, sinceenum class
doesn't convert to anything implicitly and therefore won't output an integer value.Enable
c++11
causeenum class
are a c++11 feature suing the --std=c++11
compiler flag.Overload the
<<
operator if you want tocout
anApple::color
Something like the following should work: