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

标签 c++ visual-studio class visual-c++ compiler-errors

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

  • 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/

    相关文章:

    c++ - 具有条件和无锁通知的可移植事件检查器

    c++ - 无限循环发生在哪里?

    c++ - 如何正确初始化模板类型的成员变量?

    java - 在 Windows 10 (Intellij IDEA) 上使用 gradle 项目安装 Google or-tools

    java - 在 main 中调用一个类

    c++ - 依赖于其他库的库的 Libtool 版本控制

    c++ - 是否有用于使用 gcc 构建 MS Visual C++ Express 的免费开源工具?

    .net - 我在哪里可以找到一个 .xsd 文件来为温莎城堡提供智能感知?

    Python 类变量列表与 int 不一致的行为

    Java随机数问题。