gpt4 book ai didi

c++ - [错误]请求的 'area'中属于非类类型 'r'的 'float'成员意味着什么?

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

我是编码新手,正在尝试编写具有多态性但我的矩形部分无法正常工作的程序。我试图在所有三个文件中添加变量以容纳它,但是我一直在出错。下面的代码是我最近为解决此问题的尝试。我现在得到的错误是对非类类型“float”的成员“r”中的成员“区域”和对非类类型“float”的参数“r”中的“错误”请求。我不知道如何解决这一点。如果可以的话请帮忙!

Main.cpp

#include <iostream>
#include "shape.h"
#include "shape.cpp"

using namespace std;
int main() {
float r, a, b, a1, b1;
cout<<"This program will ask you to input some data in order to find the area and the parameter of 3 shapes."<<endl;
cout<<"\nInput the circles radius --everything should be in inches (i.e 5):";
cin>>r;
Circle c(r);
cout<<"\nPlease input two side of the Right Triangle excluding the hypotenuse-- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
RTriangle rt(a,b);
cout<<"\nPlease input two side of the Rectangle -- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
Rectangle r(a1,b1);
cout<<"\n\nThe Circles Area is:"<<c.area()<<" inches, The Parameter is:"<<c.parameter()<<" inches"<<endl;
cout<<"The Rectangle Area is:"<<r.area()<<" inches, The Parameter is:"<<r.parameter()<<endl;
cout<<"The Right Triangle Area is:"<<rt.area()<<" inches, The Parameter is:"<<rt.parameter()<<" inches"<<endl;
cout<<"Thanks once agin for using this program for your AREA and PARAMETER needs!"<<endl;
system ("PAUSE");
return 0;
}

Shape.cpp
#include"shape.h"

Shape::Shape(){
sideA = sideB = 0;
}

Shape::Shape(int a, int b){
sideA = a;
sideB = b;
}

//these will get overrided
float Shape::area(){return 0;}
float Shape::parameter(){return 0;}
//rectangle definations
Rectangle::Rectangle(float a, float b):Shape(a,b){
//calling parent class constructor
}

float Rectangle::area(){
return sideA*sideB;
}

float Rectangle::parameter(){
return 2*(sideA+sideB);
}

//right triangle definations
RTriangle::RTriangle(float h, float w):Shape(h, w){
}

float RTriangle::area(){
return 0.5*sideA*sideB;
}
float RTriangle::parameter(){
float hyp = sqrt(sideA*sideA + sideB*sideB);
return sideA + sideB + hyp;
}

//circle definations
Circle::Circle(float r){
sideA = r;
}

float Circle::area(){
return 3.14 * sideA * sideA;
}

float Circle::parameter(){
return 2 * 3.14 * sideA;
}

形状
#ifndef SHAPES
#define SHAPES
#include<cmath>

class Shape {
protected:
float sideA, sideB;
float radius;
public:
Shape();
Shape(int,int);
virtual float area();
virtual float parameter();
};

class Rectangle : public Shape{
public:
Rectangle(float a, float b);
float area();
float parameter();
};

class RTriangle : public Shape{
public:
RTriangle(float h, float w);
float area();
float parameter();
};

class Circle : public Shape{
public:
Circle(float r);
float area();
float parameter();
};

#endif

最佳答案

看看Main.cpp,您会发现两行,例如

float r, a, b, a1, b1;


Rectangle r(a1,b1);

如您所见, r定义了两次。我建议用 Rectangle r(a1,b1);替换 Rectange rect(a1, b1);这应该有所帮助。

关于c++ - [错误]请求的 'area'中属于非类类型 'r'的 'float'成员意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41021586/

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