VERSION-1:Использование переменной перечисления в качестве члена данных класса выдает ошибку
// In this, the enum is declared globally
#include <iostream>
#include <string>
using namespace std;
enum Hand {RIGHT,LEFT};
class Batsman {
public:
Batsman(string s, Hand h) {
name = s;
hand = h;
}
void setName(string s) {
name = s;
}
void setHand(Hand h) {
hand = h;
}
string getName() {
return name;
}
Hand getHand() {
return hand;
}
private:
string name;
Hand hand;
};
void main() {
Batsman B1("Ryder",LEFT);
Batsman B2("McCullum",RIGHT);
}
VERSION-2:
// In this, the enum is declared inside the class
#include <iostream>
#include <string>
using namespace std;
class Batsman {
public:
enum Hand {RIGHT,LEFT};
Batsman(string s, Hand h) {
name = s;
hand = h;
}
void setName(string s) {
name = s;
}
void setHand(Hand h) {
hand = h;
}
string getName() {
return name;
}
Hand getHand() {
return hand;
}
private:
string name;
Hand hand;
};
void main() {
Batsman B1("Ryder",LEFT);
Batsman B2("McCullum",RIGHT);
}
Ошибки:
D:\\Work Space\\C++\\C.cpp: In function `int main(...)':
D:\\Work Space\\C++\\C.cpp:33: `LEFT' undeclared (first use this function)
D:\\Work Space\\C++\\C.cpp:33: (Each undeclared identifier is reported only once
D:\\Work Space\\C++\\C.cpp:33: for each function it appears in.)
D:\\Work Space\\C++\\C.cpp:34: `RIGHT' undeclared (first use this function)
Просьба сообщить мне исправления в обоих случаях, чтобы я мог разобраться в концепции раз и навсегда. Будем очень благодарны.
"Errors"? Убедитесь, что вы указали точные сообщения об ошибках. – crashmstr
Добавьте ошибки, которые производятся ... мы не являемся компиляторами. – jepio
Это 'int main'. – chris