我查看了所有这些并且对编码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;
}
由于 大卫
答案 0 :(得分:2)
现在我看到你在基类中有一个名为Radius
的成员,它与派生类相同,这就是导致错误的原因。解决方案是使用基类名称对其进行限定:
变化:
return pi * (Radius * Radius);
为:
return pi * (Circle::Radius * Circle::Radius);
这个附加:double radius;
可能来自某些测试 - 对吧?
[编辑]
从设计的角度来看,class Radius : public Circle
的存在毫无意义,只需使用Circle
就可以了解它。
答案 1 :(得分: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;
}