2016-12-07 6 views
0

私はコーディングが新しく、多形性を持つプログラムを作成しようとしていますが、四角形の部分が機能しません。 3つのファイルすべてに変数を追加しようとしましたが、エラーが発生しています。以下のコードはこれを修正する私の最近の試みです。私が今取得しているエラーは、非クラス型 'float'である 'r'のメンバ 'area'と非クラス型 'float'である 'r'のパラメータに対する[error]要求です。私はこの時点でこれをどのように修正するかについて、迷っています。できれば助けてください!Wahtは 'class'型ではない 'r'のmemebr 'area'に対する[error]要求をしていますか?

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; 
} 

Shape.h

#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 

答えて

0

Main.cppを見てみましょう、あなたは

float r, a, b, a1, b1; 
のような2つの行を検索します

Rectangle r(a1,b1); 

あなたはrが二度定義されて見ることができるように。 Rectangle r(a1,b1);Rectange rect(a1, b1); に置き換えることをお勧めします。

+0

それは働いた!ありがとう!それは簡単な修正だった –

関連する問題