从类定义传递字符串指针

passing string pointers from class definition

本文关键字:字符串 指针 定义      更新时间:2023-10-16

我遇到的问题是字符串参数。我不知道如何使用它。我只想分类从开始到用户输入都有未定义的长度字符串。我得到的错误是,当我键入字符串分类时,"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" ) 
    {
    } 
};

您正在重新定义名为分类string。您只需在类声明中声明该变量一次,即可在所有成员函数中使用该变量。您的参数也使用了相同的名称,这既令人困惑又危险。

您还应该小心处理指针,因为您似乎不确定何时使用它们,或者使用引用。如果您确实尝试像这样比较string* classification参数if (classification == "triangle" ),您会意识到您无法将std::string*与constchar*进行比较

理想情况下,您应该在这里使用枚举,就像这样。

class Shape
{
  public:
    enum Classification { SQUARE, TRIANGLE, CIRCLE };
}
Shape::Area(float side, float height, Classification shapeClass)
{
  if(shapeClass == SQUARE) {} // Etc
}

甚至比使用继承多态性以及area() 等重载函数更好

class Shape { virtual float Area(); };
class Triangle : public Shape { virtual float Area(); };

在代码Shapes::area中,您不需要重新定义类型为std::string的变量classification,因为其中一个参数是string *classification

您可以使用您的参数*classification == "circle",前导*是因为您将参数声明为指针类型。一个替代方案是在C++中将classification声明为string &classification,这是一个引用。有了参考参数,您可以像classificaiton == "circle"一样直接使用。

希望能有所帮助。

将字符串参数传递给类成员的正确方法是什么?

不要。您已经可以访问类的方法中的成员,不需要额外的变量,也不需要将其作为参数传递。

float Shapes::area(float side, float height, string *classification)
{
    string classification;

在这种情况下,您有一个名为classification的参数具有相同名称的成员带有相同名称的局部变量

仔细想想,你甚至不需要指点。

你需要的是读一本C++书。我既不讽刺也不刻薄。C++很难做到正确,而且在理解变量或范围的更基本的概念之前,您似乎已经开始使用指针了。

您的参数名称与类成员相同。它们不是同一个变量。

如果你想让类成员成为参数的影子副本,你应该这样做:

float Shapes::area(float side, float height, string classification)
{
     float area=0.0;
     this->classification = classification;
     getline(cin,classification);
     //rest goes here
}

这里,this是指向holder类的指针。

您需要更改类声明:

class Shapes
{
    string classification;
}

除非真的有必要,否则不要使用指向类的指针。如果要更改值,请使用引用。

附言:不要在函数定义的末尾加分号。