Go Down

Topic: enumeration declaration (Read 1 time) previous topic - next topic

PaulS

Quote
eclipse understands that function foodbar belongs to datatype  food

Then it (or you) understand it all wrong. The function is declared to return a value of type food, which is an enum.

Quote
but foodbar returns something so it makes a warning.

Then, eclipse is wrong. Specifically, what warning?

Quote
also you could say that the foodbar functions returns a food variable

Again, you'd be wrong. It returns a value of type food, not a variable of type food. What you do with that value is up to you. You can store it in a variable of type food, or print it, or pass it to a function that expects a food.


kyrpav

yes thank you and sorry about my english i meant that it returns a value of type food. and i do not get why it give warning:

Description   Resource   Path   Location   Type
No return, in function returning non-void   main.cpp /testing/src   line 22   Code Analysis Problem

johncc


ok thank you i have one more question. eclipse understands that function foodbar belongs to datatype  food and it is a non-void function , and foodbar returns something but eclipse does not understand it and it gives warning.



Eclipse is complaining because I should have written something more like:
Code: [Select]


enum food { orange, apple, watermelon, unknown };

food foodbar(const char * x){
if ( !strcmp(x, "orange")) return orange;
if ( !strcmp(x, "apple")) return apple;
if ( !strcmp(x, "watermelon")) return watermelon;
return unknown;
}

PaulS

Quote
No return, in function returning non-void   main.cpp /testing/src   line 22   Code Analysis Problem

means that not all paths properly return a value. You need to add another value, None, to the enum and return that at the end of the function, if no other return path is followed.

kyrpav

and this was because you did not have else at the last if? this is like a default situation in switch() i think.

Go Up