enum and incompatibility between declaration & definition

Hi all,

Please consider these two codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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?
Change line 13 to:

Book::Genre Book::GENRE() {
Thank you very much. It solved my problem but may you please tell me why we should define GENRE() like that?
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.
Last edited on
Ow. Ok. Thank you very much.
Topic archived. No new replies allowed.