class Book {
public:
enum Genre {
fiction = 1, nonfiction, periodical, biography, children
};
Genre GENRE() {
cout << "\nEnter the Genre of the BOOK in groups of {fiction, nonfiction}";
string s;
cin >> s;
if ( s == "fiction")
return fiction;
if ( s == "nonfiction")
return nonfiction;
return periodical;
}
};
//****************************************
int main() {
Book b;
cout << b.GENRE();
getch();
return 0;
}
I run it without problem but when I want to define the Genre GENRE() in outside of the class as following:
class Book {
public:
enum Genre {
fiction = 1, nonfiction, periodical, biography, children
};
Genre GENRE();
};
//***************************************
Genre Book::GENRE() {
cout << "\nEnter the Genre of the BOOK in groups of {fiction, nonfiction}";
string s;
cin >> s;
if ( s == "fiction")
return fiction;
if ( s == "nonfiction")
return nonfiction;
return periodical;
}
//****************************************
int main() {
Book b;
cout << b.GENRE();
getch();
return 0;
}
I get many errors about incompatibility between declaration and definition and .... It's not reasonable but I don't know why?
I don't want define that function inside of the class because it makes the class messy.
Any Idea?
The issue is not GENRE() pe se, but it's return type. The type Genre is within the scope of Book (it is not global), therefore when you reference the type, you must specify the scope.