gpt4 book ai didi

c++ - 错误: “expression must have class type” & “left of must have class/struct/union”

转载 作者:行者123 更新时间:2023-12-02 11:14:11 24 4
gpt4 key购买 nike

我有一个程序正在使用用户指定的测量值来计算矩形的面积。出于特定原因,我正在使用一个类来执行此操作,但是我的编译器会生成两个错误...

  • expression must have class type
  • left of '.getArea' must have class/struct/union

  • 我该如何解决?

    Rectangle.h
    class Rectangle
    {
    private:
    int length;
    int width;
    int area = length * width;

    public:
    Rectangle(int l, int w);
    int getLength();
    void setLength(int l);
    int getWidth();
    void setWidth(int w);
    int getArea();
    void setArea(int a);
    };

    Rectangle.cpp
    Rectangle::Rectangle(int l, int w)
    {
    length = l;
    width = w;
    }
    --some code--
    int Rectangle::getArea()
    {
    return area;
    }
    void Rectangle::setArea(int a)
    {
    area = a;
    }

    Area.cpp
    int i, lth, wth;

    for (i = 0; i < 3; i++)
    {
    cout << "Enter your measurements, length first" << endl;
    cin >> lth >> wth;
    Rectangle rMeasure(int lth, int wth);
    cout << "Area of this rectangle is: " << rMeasure.getArea(); //problem code
    }

    最佳答案

    这个

    Rectangle rMeasure(int lth, int wth);

    是一个函数声明。

    看来你是说
    Rectangle rMeasure(lth, wth);

    关于c++ - 错误: “expression must have class type” & “left of must have class/struct/union” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757720/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com