catching polymorphic type 'class std::exception' by value [-Wcatch-value=]


class Person{
public:
    Person(){
        mAge = 0;
    }
    void setAge(int age){
        if(age < 0 || age > 100){
            throw out_of_range("年龄应该在0-100之间!");
        }
        this->mAge = age;
    }
public:
    int mAge;
};

void test01(){
    Person p;
    try{
        p.setAge(1024);
    }catch(out_of_range& e){//此处报错
        cout << e.what() << endl;
    }
}

gcc编译报错catching polymorphic type 'class std::exception' by value [-Wcatch-value=];


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM