我遇到的问题是字符串参数。我不确定如何使用它。我只希望分类从一开始就具有未定义的长度字符串,直到用户输入。我得到的错误是'std :: string classification'的声明在我输入字符串分类时会隐藏一个参数。将字符串参数传递给类成员的正确方法是什么?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Shapes
{ //Begin Class Definition
private:
float side;
float height;
int exponent;
string *classification;
public:
Shapes(float side, float height, string * classification);
//CONSTRUCTOR
~Shapes(){};
//DESTRUCTOR
float area(float side, float height, string *classification);
float perimeter(float side, float height, string *classification);
}; // End Class Definition
int power(float side, int exponent)
{
int i;
int total[exponent];
float sum;
for ( i = 0 ; i < exponent ; i ++ )
{
total[i]= side;
sum *= total[i] ;
}
return sum;
}
float Shapes::area(float side, float height, string *classification)
{
float area=0.0;
string classification;
getline(cin,string);
if (classification == "square" )
{
area = power(side,2);
return area;
}
if (classification == "triangle" )
{
area = (side* height) / 2 ;
return area;
}
if (classification == "hexagon" )
{
float constant = 2.598706;
area= constant * power(side,2);
return area;
}
if (classification == "circle" )
{
}
};