Enum as return value from a function!?

I worked on the following code for a while and I encounter always the same compilation error.

The code is not complete.

Anyway, why can't I have a enum as a return value for a function?

Here is my code:
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
32
33
34
35

#include <iostream>
using namespace std;
string B;
class Broetchen
{
	private:
		string Belag;
		bool Butter;
		
	public:
		void setBelag(string);
		string getBelag();
		enum Teigware {Mohn, Sesam, Laugen, Kaese, Roggen};
		void setT(Teigware);
		Teigwaren getT();
};
string Broetchen::getBelag()
{
	return Belag;
}
void Broetchen::setBelag(string B)
{
	Belag = B;
}
int main()
{
Broetchen Wurschtweck;
cout << "Welchen Belag hätten Sie gerne?";
cin >> B;
Wurschtweck.setBelag (B);
cout << "mit " << Wurschtweck.getBelag() << "\n";
return 0;
};


This is the error message:

1
2
3
tobias@Lonestar:~/c++$ g++ SisV2.cpp -Wall
SisV2.cpp:15:3: error: ‘Teigwaren’ does not name a type


Thanks in advance for your help! Tobi :)
The name of your type is Teigware, not Teigwaren
Teigwaren is not a valid return type. If that is another class, then you'll need to include it in your include section. You may also want to include string so that you can use that string at the top.

Why did you writ all the code in German, if you don't mind me asking. Also, a suggestion for line 29 "Welchen Belag gefällt Sie?".
Does the 4-digit number with 25 as the two left-most digits signifies something in Germany? Or is it just a coincidence?
Thanks for your answers.

Well, I am a student at the Universität Heidelberg and there is an upcoming exam in c++. This is why I try to solve some of the exercises provided by out lecturer.

Here we have to program a class that creates a sandwich: You have different types of bread, you can add a topping and you are asked if you want to have butter on your sandwich. Dr. Kondermann wants us to use ENUM for the bread types and this is where I am struggling.

I changed the ENUM in line 14 to Teigwaren like Cubbi suggested, but when I created the following code to set and get a variable back I get error messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
	private:
		Teigwaren Teigware;
		string Belag;
		bool Butter;
		
	public:
		void setBelag(string);
		string getBelag();
		enum Teigwaren {Mohn, Sesam, Laugen, Kaese, Roggen};
		void setT(Teigwaren);
		Teigwaren getT();
};

Teigwaren Broetchen::getT();
{
	return Teigware;
}	
string Broetchen::getBelag()
{
	return Belag;
}


Error message :

1
2
3
4
5
tobias@Lonestar:~/c++$ g++ SisV2.cpp -Wall
SisV2.cpp:7:3: error: ‘Teigwaren’ does not name a type
SisV2.cpp:19:1: error: ‘Teigwaren’ does not name a type
SisV2.cpp:20:1: error: expected unqualified-id before ‘{’ token


So my question is: Is "Teigwaren" not a type like int, float or bool?

What am I doing wrong here?
It is a type, like int float or bool, but its full name is Broetchen::Teigwaren

Between Broetchen { and }; you can refer to it as simply Teigwaren, but outside those braces, the name is Broetchen::Teigwaren

This is the same as how you had to write Broetchen::getT after the }, but only getT inside
Thanks for your help!

But I still don't get it!

Are you suggesting that if I want to declare a variable, it must look like this:

1
2
3
4
5

private:
		string Belag;
		Broetchen::Teigwaren Teigware;


This leads to the following error:

SisV3.cpp:9:3: error: ‘Teigwaren’ in ‘class Broetchen’ does not name a type


Oh, right, I was referring to the second error on the list. Here's the whole thing, made to compile:

changes:
1. Declare Teigwaren before the first use
2. Chagne Teigwaren to Broetchen::Teigwaren when used outside class body
3. Remove stray semicolon after getT()

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
32
33
#include <iostream>
using namespace std;
string B;
class Broetchen
{
        public:
                enum Teigwaren {Mohn, Sesam, Laugen, Kaese, Roggen};
        private:
                Teigwaren Teigware;
                string Belag;
                bool Butter;

        public:
                void setBelag(string);
                string getBelag();
                void setT(Teigwaren);
                Teigwaren getT();
};

Broetchen::Teigwaren Broetchen::getT()
{
        return Teigware;
}
string Broetchen::getBelag()
{
        return Belag;
}

int main()
{
    Broetchen b;
    Broetchen::Teigwaren a = b.getT();
}
one word: AWESOME!

Thank you!!
Topic archived. No new replies allowed.