【问题标题】:expected primary-expression before '*'?'*' 之前的预期主表达式?
【发布时间】:2016-06-02 12:18:48
【问题描述】:

我已经浏览了所有这些,并且对 c++ 编码相对较新,只是不知道我缺少什么。有什么想法吗?

我的错误发生在第 45 行“return pi * (Radius * Radius);”我几乎肯定该行的语法是正确的,但为什么会出现编译错误。

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

谢谢 大卫

【问题讨论】:

  • 我认为这里在理解 OOP 设计方面存在一些问题。 Radius到底为什么继承自Circle
  • 这段代码很混乱。 Circle 类同时具有 Radiusradius 类成员。构造函数初始化私有Radius。公共radius 根本没有初始化。

标签: c++


【解决方案1】:

现在我看到您在基类中有一个成员,其名称为 Radius,与派生类中的名称相同,这就是导致错误的原因。解决方案是使用基类名称对其进行限定:

改变:

return pi * (Radius * Radius);

到:

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

这个附加:double radius; 可能来自一些测试 - 对吧?

[编辑]

从设计的角度来看,class Radius : public Circle 的存在意义不大,使用Circle 来获得它的辐射应该没问题。

【讨论】:

  • 感谢 cmets,是的,我正在尝试执行验证检查并忘记将其删除。我也没有看到已经公开的对'circle'的循环引用。关于派生类和继承,我还有很多学习要做。
  • @haynstyle 没问题,我通常从不使用以大写字母开头的变量,这些名称是我为类保留的。我发现你也可以在类体中使用using Circle::Radius; 来让 Radius 引用基类变量,但是这种方法还有其他缺点。
  • 很高兴知道,这非常有用,我会将这些输入应用到我的下一个项目中。再次感谢。
【解决方案2】:
#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;
}

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2020-01-16
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2013-08-29
    相关资源
    最近更新 更多