简体   繁体   中英

expected primary-expression before '*'?

I have looked through all of these and am relatively new to coding c++ and just don't know what I am missing. Any idea's?

My error occurs at line 45 "return pi * (Radius * Radius);" I am almost positive that the syntax for that line is correct but why am i getting compile errors.

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

Thanks David

Now I see you have a member in base class which has a name Radius which is the same as in derived class, this is what is causing an error. The solution is to qualify it with base class name:

change:

return pi * (Radius * Radius);

to:

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

this additonal: double radius; is probably from some testing - right?

[edit]

From design point of view the existance of class Radius : public Circle makes little sense, it should be fine to just use Circle to get its radious.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM