I’m a bit confused with an error I’m having right now. I have a getter/setter in my class. The setter takes an enum as a parameter and the getter should return this enum. However, I’m getting this error for the getter:
error: C2143: syntax error : missing ‘;’ before ‘Session::type’
as if SessionType was undefined. However I’m not getting the same error for the setter. Is there any reason why? And is there any way to fix this error?
(by the way, it compiles fine if I return an int but I’d rather keep the getter consistent with the setter)
Here is my code:
Session.h
class Session {
public:
enum SessionType {
FreeStyle,
TypeIn,
MCQ
};
explicit Session();
SessionType type() const;
void setType(SessionType v);
private:
SessionType type_;
}
Session.cpp:
SessionType Session::type() const { // ERROR!!
return type_;
}
void Session::setType(SessionType v) { // No error?
if (type_ == v) return;
type_ = v;
}
The problem is, when you’re defining the function in Session.cpp, at the time of the evaluation of the return type, the compiler is not quite yet aware that it’s a member function of the class, and doesn’t have that enumeration in scope. It has to do with the left-to-right definition of the function. Try this
Note, the other case worked because it doesn’t encounter the enumeration until it’s evaluated the function name, and therefore has the enumeration in scope.
Also, the error you’re getting is due to the lack of a semicolon at the end of the class definition.