从类定义传递字符串指针
passing string pointers from class definition
我遇到的问题是字符串参数。我不知道如何使用它。我只想分类从开始到用户输入都有未定义的长度字符串。我得到的错误是,当我键入字符串分类时,"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;
}
除非真的有必要,否则不要使用指向类的指针。如果要更改值,请使用引用。
附言:不要在函数定义的末尾加分号。
- strcpy 在类中无法使用字符串指针
- 如何将字符串指针数组转换为字符串类型的智能指针向量?
- 使用conststd::字符串指针作为函数参数
- 将字符串指针传递到C++和Xcode 11.1中不同线程上运行的函数
- OOP 和字符串指针的奇怪行为
- 在 C++ 中通过字符串指针复制字符串变量中的字符串
- 删除字符串指针在struct中的内存
- 字符串指针的排序向量
- C#C /CLR处理REF参数和字符串指针
- 问题在字符串指针中存储单词
- 如何在C++中从字符串指针中获取字符
- 如何使用字符串指针访问映射的键
- 将字符串指针与 NULL 以及 NULL 字符进行比较的目的
- 如何获取字符串指针的值?
- C++ 字符串* 指针未在控制台中正确打印
- Visual Studio C++ 空字符串指针
- 管理C#中返回的字符串指针
- 指向C++中的字符串指针数组的指针
- 如何将结果集设置为字符串指针
- 如何初始化字符串指针