请帮助在继承#constructor_error上调试这个c ++代码

时间:2015-01-20 21:01:31

标签: c++ debugging

此代码中有错误,请帮我解决。

    #include <iostream> 
    #include <string> 
    #include <iomanip> 
    using namespace std; 
    class base 
        protected:
                double area; 
                string p_name; 
                int n_side;
                void show_out();
                base(string,int); 
    };
    class square:public base { 
        private:
                double length,width; 
        public: 
                void get_data();
                void cal_sarea(); 
                square(); 
    }; 

    class triangle:public base { 
        private:
                double base,height;
        public:
                void get_data(); 
                void cal_tarea();
                triangle();  
    }; 
    int main() {
        square sob1; 
        triangle tob1;  
            sob1.get_data(); 
            sob1.cal_sarea(); 
            tob1.get_data(); 
            tob1.cal_tarea(); 
     return 0; 
    }
    void base::show_out() { 
                        cout<<setiosflags(ios::fixed|ios::showpoint)                   <<setprecision(2)<<area<<endl;
    }
    void square::get_data() {
                        cout<<"please enter length"<<endl; 
                        cin>>length; 
                        cout<<"please enter width"<<endl;
                        cin>>width;
    }
    void square::cal_sarea() {
            area = length * width; 
            show_out();
    }
    void triangle::get_data() {
                        cout<<"please enter base of triangle"<<endl;
                        cin>>base; 
                        cout<<"please enter height of the triangle"<<endl;
                        cin>>height; 
}
void triangle::cal_tarea() {
            area = 0.5 * base * height; 
            show_out(); 
    }
    base::base(string name,int num)
     :p_name(name),n_side(num) {}

    square::square() 
       :base("square",4){} 

    triangle::triangle()  //  gives a errors here, it says no matching fucntion to call to base::base() 
         :base("triangle",5) {}

1 个答案:

答案 0 :(得分:1)

您的triangle - 类有一个名为base的成员变量,并且还继承自名为base的类。

编译器让这些名字变得混乱。

以与基类名称不同的方式命名您的成员变量 每个项目都必须具有 唯一 名称。