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