[英]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. 我已经仔细阅读了所有这些内容,并且对使用c ++进行编码还比较陌生,只是不知道自己缺少什么。 Any idea's?
有任何想法吗?
My error occurs at line 45 "return pi * (Radius * Radius);" 我的错误发生在第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. 现在,我看到您在基类中有一个成员,该成员的名称
Radius
与派生类中的名称相同,这是导致错误的原因。 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;
这个附加的:
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. 从设计的角度来看,
class Radius : public Circle
的存在class Radius : public Circle
没什么意义,仅使用Circle
来获取其半径应该没问题。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.