简体   繁体   中英

C++ enum type as a function

I am working on C++ code trying to build a FlipIt game program. I have a header file and a .cpp file. In the header file I have a flipit class where one of the functions is an enum type function. For example in the header file FlipIt.h:

(enum declaration)

enum  Color  { clear_ = false, solid_ = true };

(the function)

Color  fetch( int  row, int  col ) const;

(In the FlipIt.cpp file this is what I implemented the function in the class as)

int FlipIt::fetch( int  row, int  col ) const

When I do this VS2010 says IntelliSense: declaration is incompatible with "FlipIt::Color FlipIt::fetch(int row, int col) const"

What does this mean?

But Color is not int . It's enum and it has some underlying type (that's depends on compiler), but it's another type anyway.

It means that your promised returnvalue of FlipIt::Color in your declaration of your method differs from the type you provide in your implementation. And enum is not int .
Try

FlipIt::Color FlipIt::fetch(int row, int col) const { } 

for your implementation.

You must distinguish the enum type was defined in which namespace.if your enum was just defined in header file, not in cpp file. You include the header file, the linker can find the type of definition, if you define in class, you must add class prefix. The linker can find the definition of the type. So, if your enum type was defined in global area there is no problem. if you define in class, you must use "Class::Type".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM