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=];

posted @ 2019-09-02 18:35  江凌何以万里  阅读(1842)  评论(1编辑  收藏  举报