gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:38:32 26 4
gpt4 key购买 nike

<分区>

我写了一个简单的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;
}

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

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;

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

谢谢!

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