2016-06-02 16 views
1

Ich habe alle diese durchgeschaut und bin relativ neu in der Codierung von C++ und weiß einfach nicht, was ich vermisse. Irgendwelche Ideen?erwarteter primärer Ausdruck vor '*'?

Mein Fehler tritt bei Zeile 45 "Return Pi * (Radius * Radius);" Ich bin fast sicher, dass die Syntax für diese Zeile korrekt ist, aber warum bekomme ich Kompilierfehler.

#include <iostream> 
#include <cstdlib> 

using namespace std; 
const double pi = 3.14159; 

class Rectangle 
{ 
protected: 
    float length, width; 
public: 
    Rectangle(): length(0), width(0) 
    { 
     cout<<"Enter length: "; cin>>length; 
     cout<<"Enter width: "; cin>>width; 
    } 
}; 

class Circle 
{ 
    protected: 
    float Radius; 
public: 
    double radius; 
    Circle(): Radius(0) 
    { 
     cout<<"Enter Radius: "; cin>>Radius; 
    } 
}; 

class Area : public Rectangle 
{ 
public: 
    float getArea() 
    { 
     return length*width; 
    } 
}; 

class Radius : public Circle 
{ 
    public: 
    float getRadius() 
    { 
     return pi * (Radius * Radius); 
    } 
}; 

int main() 
{ 
char choice; 
for (int i = 0; i < 4; i++) //loop statement 
{ 
cout << "Program to Find Area of a Square and Circle" << endl <<       //selection of which calculation to run 
     "Enter S for square square." << endl << 
     "Enter C for circle." << endl << 
     "Enter Q to Quit the program." << endl << endl << 
     "Enter an option above: "; 
cin >> choice; 

switch(choice) 
    { 
    //Square option: 
    case 'S': 
    case 's': { 
     cout<<"Enter data for rectangle to find area.\n"; 
     Area a; 
     cout<<"Area = "<<a.getArea()<<" square\n\n"; 
     break;} 

    //Circle option: 
    case 'C': 
    case 'c': { 
     cout<<"Enter data for circle to find radius.\n"; 
     Radius c; 
     cout<<"Radius = "<<c.getRadius()<<" meter\n\n"; 
     break;} 

    //Quit option: 
    case 'Q': 
    case 'q': { 
     cout << "Thank you for using Area Application" << endl << endl; 
     system("PAUSE"); 
     return EXIT_SUCCESS; 
    break;} 

    //default option binds to a non-selected choice function: 
    default: 
     cout << choice << " is not a valid selection." << endl; 
     cout << "Select a valid shape choice: S or C" << endl << endl; 
    break; 
    } 
} 

cout << "Press enter to continue ..." << endl; 
return EXIT_SUCCESS; 
} 

Dank David

+3

Ich denke, es gibt einige Probleme hier in Bezug auf das Verständnis OOP Design. Warum in aller Welt erbt "Radius" von "Circle"? – Smeeheey

+0

Dieser Code ist sehr verwirrt. Die Klasse "Circle" hat die Klassenmitglieder "Radius" und "Radius". Der Konstruktor initialisiert den privaten 'Radius'. Der öffentliche 'Radius' ist überhaupt nicht initialisiert. –

Antwort

2

Jetzt sehe ich Sie ein Mitglied in der Basisklasse haben, die einen Namen hat Radius die die gleichen wie in abgeleiteten Klasse ist, das ist, was einen Fehler verursacht. Die Lösung ist es mit der Basisklasse Namen zu qualifizieren:

Änderung:

return pi * (Radius * Radius); 

zu:

return pi * (Circle::Radius * Circle::Radius); 

diese additonal: double radius; ist wahrscheinlich von einigen Tests - nicht wahr?

[Bearbeiten]

Aus Sicht des Designs die Existenz von class Radius : public Circle wenig Sinn macht, sollte es in Ordnung sein, nur Circle nutzen, um ihre radious zu bekommen.

+0

Danke für die Kommentare, ja, ich habe versucht, eine Validierungsprüfung durchzuführen und vergessen, es zu entfernen. Ich sah auch nicht den Zirkelbezug zum "Kreis", der bereits öffentlich war. Ich habe immer noch viel zu lernen über abgeleitete Klassen und Vererbung. – haynstyle

+0

@haynstyle kein Problem, ich benutze normalerweise nie Variablen, die mit Großbuchstaben beginnen, diese Namen, die ich für Klassen reserviert habe. Ich fand, dass Sie auch 'using Circle :: Radius;' im Klassenkörper benutzen können, um Radius auf die Basisklassenvariable zu beziehen, aber dieser Ansatz hat andere Nachteile. – marcinj

+0

Gut zu wissen, das ist sehr nützlich, und ich werde diesen Input übernehmen und auf mein nächstes Projekt anwenden. Danke noch einmal. – haynstyle

0
#include <iostream> 
#include <cstdlib> 

using namespace std; 
const double pi = 3.14159; 

class Rectangle 
{ 
protected: 
    float length, width; 
public: 
    Rectangle(): length(0), width(0) 
    {cout<<"Enter length: "; cin>>length;cout<<"Enter width: "; cin>>width;} 
    float getArea(){return length*width;} 
}; 

class Circle 
{ 
    protected: 
    float Radius; 
public: 
    Circle(): Radius(0)  {cout<<"Enter Radius: "; cin>>Radius;} 
    float getRadius()  {return pi * (Radius * Radius);} 
}; 

int main() 
{ 
char choice; 
for (int i = 0; i < 4; i++) //loop statement 
{ 
cout << "Program to Find Area of a Square and Circle" << endl <<       //selection of which calculation to run 
     "Enter S for square square." << endl << 
     "Enter C for circle." << endl << 
     "Enter Q to Quit the program." << endl << endl << 
     "Enter an option above: "; 
cin >> choice; 

switch(choice) 
    { 
    //Square option: 
    case 'S': 
    case 's': { 
     cout<<"Enter data for rectangle to find area.\n"; 
     Rectangle a; 
     cout<<"Area = "<<a.getArea()<<" square\n\n"; 
     break;} 

    //Circle option: 
    case 'C': 
    case 'c': { 
     cout<<"Enter data for circle to find radius.\n"; 
     Circle c; 
     cout<<"Radius = "<<c.getRadius()<<" meter\n\n"; 
     break;} 

    //Quit option: 
    case 'Q': 
    case 'q': { 
     cout << "Thank you for using Area Application" << endl << endl; 
     system("PAUSE"); 
     return EXIT_SUCCESS; 
    break;} 

    //default option binds to a non-selected choice function: 
    default: 
     cout << choice << " is not a valid selection." << endl; 
     cout << "Select a valid shape choice: S or C" << endl << endl; 
    break; 
    } 
} 

cout << "Press enter to continue ..." << endl; 
return EXIT_SUCCESS; 
} 
Verwandte Themen