-2

我正在尝试 c++,仍然是大学的学生,无法发现错误来自哪里。有人可以帮忙吗?

class  M() {
    static  bool m() {  
        cout << "Hello, do you want to tell me your name (y or n)";
        char answer = 0;
        int  times = 1;
        while(times < 3) {
            cin >> answer;
            switch(answer){
                case 'y' : 
                    return true;
                case 'n' : 
                    return false;
                default :  
                    cout << "I am sorry, I don't understand that.";
                    times += 1;
            }
            cout << "Your time's up.";
            return false;
        }    
    }
}

int main() {
    M::m();
};
4

1 回答 1

1

它在这条线上:

class M() {

您不要在类名定义之后放置方括号。将其更改为:

class M {

您的代码还有一些问题(类结束大括号后的分号等),工作代码如下所示:

class  M {
public:
    static bool m() {
        std::cout << "Hello, do you want to tell me your name (y or n)";
        char answer = 0;
        int  times = 1;
        while(times < 3) {
            std::cin >> answer;
            switch(answer){
                case 'y' :
                    return true;
                case 'n' :
                    return false;
                default :
                    std::cout << "I am sorry, I don't understand that.";
                    times += 1;
            }
            std::cout << "Your time's up.";
            return false;
        }
        // You need this so you won't get warnings.
        return false;
    }
}; // Don't forget this semicolon!

int main() {
    M SomeObject;
    SomeObject::m();
};
于 2012-12-15T01:14:39.203 回答