为什么可以将枚举作为函数变量传递而不是返回枚举?

lau*_*ent 1 c++ enums class public

我对我现在遇到的错误感到有点困惑.我班上有一个getter/setter.setter将枚举作为参数,getter应返回此枚举.但是,我得到了getter的这个错误:

错误:C2143:语法错误:缺少';' 在'Session :: type'之前

好像SessionType没有定义.但是我没有为setter获得相同的错误.有什么理由吗?有没有办法解决这个错误?

(顺便说一下,如果我返回它会编译好,int但我宁愿让getter与setter保持一致)

这是我的代码:

Session.h

class Session {

public:

    enum SessionType {
        FreeStyle,
        TypeIn,
        MCQ
    };

    explicit Session();
    SessionType type() const;
    void setType(SessionType v);

private:

    SessionType type_;

}
Run Code Online (Sandbox Code Playgroud)

Session.cpp:

SessionType Session::type() const { // ERROR!!
    return type_;
}

void Session::setType(SessionType v) { // No error?
    if (type_ == v) return;
    type_ = v;
}
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 7

更改

SessionType Session::type() const { // ERROR!!
Run Code Online (Sandbox Code Playgroud)

Session::SessionType Session::type() const {
Run Code Online (Sandbox Code Playgroud)