Evelyn_Ye10 2021-05-14 00:42 采纳率: 100%
浏览 2149
已采纳

C++ Error:明显调用的表达式前的括号必须具有(指针)函数类型

#include <iostream>
using namespace std; 

class Shape
{
protected:
    int w, h;
public:
    Shape(int w=0, int h=0):w(w), h(h){}
}; 

class Rectangle:public Shape
{
public:
    int h,w;
    double area;
    Rectangle(int n,int m){
        h=n;
        w=m;
    }
    double GetArea(){
        area=h*w;
    return area;
    }
};

class Triangle:public Shape
{
public:
    int h,w;
    double area;
    Triangle(int n,int m){
        h=n;
        w=m;
    }
    double GetArea(){
    area=(h*w)/2;
    return area;
    }
};


int main()
{
    int w, h;
    cin >> w >> h;
    Rectangle a(w , h);
    cout << a.area() << endl;
    cin >> w >> h;
    Triangle b(w, h);
    cout << b.area() << endl;
    return 0;
}

 

main函数中出现编译错误,请教大神,如何更改?

  • 写回答

3条回答 默认 最新

  • benbenli 2021-05-14 02:41
    关注

    可以调用函数GetArea()

    #include <iostream>
    using namespace std; 
    
    class Shape
    {
    protected:
        int w, h;
    public:
        Shape(int w=0, int h=0):w(w), h(h){}
    }; 
    
    class Rectangle:public Shape
    {
    public:
        int h,w;
        double area;
        Rectangle(int n,int m){
            h=n;
            w=m;
        }
        double GetArea(){
            area=h*w;
        return area;
        }
    };
    
    class Triangle:public Shape
    {
    public:
        int h,w;
        double area;
        Triangle(int n,int m){
            h=n;
            w=m;
        }
        double GetArea(){
        area=(h*w)/2;
        return area;
        }
    };
    
    
    int main()
    {
        int w, h;
        cout << "Please enter width and length of a rectangle: ";
        cin >> w >> h;
        Rectangle a(w , h);
        cout << "The area of the rectagle is: ";
        cout << a.GetArea() << endl;
        
        cout << "Please enter the side and height of a triangle: ";
        cin >> w >> h;
        Triangle b(w, h);
        cout << "The area of the triangle is: ";
        cout << b.GetArea()<< endl;
        return 0;
    }
    
    
    // Output:
    lease enter width and length of a rectangle: 4 5                                                                                                      
    The area of the rectagle is: 20                                                                                                                        
    Please enter the side and height of a triangle: 4 8                                                                                                    
    The area of the triangle is: 16 
     
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵