C++请求''中的成员'',它是非类类型''

Aru*_*run 3 c++

我编写了一个简单的 C++ 程序,如下所示-

#include<iostream>
using namespace std;

class Rectangle
{
    double length, breadth;

public:
    Rectangle(void);        // constructor overloading
    Rectangle(double, double);  // constructor of class
    // void set_values(double l, double b);
    double area(void);
};  // can provide an object name here


// default constructor of class 'Rectangle'-
Rectangle::Rectangle(void)
{
    length = 5;
    breadth = 5;
}


// constructor of class 'Rectangle'-
Rectangle::Rectangle(double l, double b)
{
    length = l;
    breadth = b;
}


/*
void Rectangle::set_values(double l, double b)
{
    length = l;
    breadth = b;
}
*/


double Rectangle::area(void)
{
    return length * breadth;
}


int main()
{
    /*
    Rectangle r;
    r.set_values(12, 3.4);
    */

    Rectangle r(12, 3.4);
    Rectangle s();

    cout<<"Area = "<<r.area()<<endl;
    cout<<"Area = "<<s.area()<<endl;


return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,出现以下错误-

Classes_Example.cpp: In function ‘int main()’:
Classes_Example.cpp:61:21: error: request for member ‘area’ in ‘s’, which is of non-class type ‘Rectangle()’
  cout<<"Area = "<<s.area()<<endl;
Run Code Online (Sandbox Code Playgroud)

我正在使用 g++ (GCC) 7.2.0

谢谢!

Sor*_*tir 6

Rectangle s();
Run Code Online (Sandbox Code Playgroud)

是函数声明而不是变量。在 C++ 中,任何可以解析为函数声明的东西都需要对变量替代进行解析。删除 () 将使其成为变量。