【发布时间】:2018-10-10 21:39:40
【问题描述】:
我正在尝试对类中的私有变量使用 std::to_string() 函数,但出现“未声明的变量”错误。
这是带有函数声明的头文件:
class Auto_Part
{
public:
Auto_Part();
Auto_Part(std::string t, std::string n, int pn, double p): type(t), name(n), part_number(pn), price(p) {};
std::string get_type() const;
std::string get_name() const;
int get_part_number() const;
double get_price() const;
void set_type(std::string);
void set_name(std::string);
void set_part_number(int);
void set_price(double);
friend std::ostream& operator<<(std::ostream&, const Auto_Part&);
bool operator<(const Auto_Part&) const;
std::string to_string();
private:
std::string type;
std::string name;
int part_number;
double price;
};
这是我的 .cpp 文件中的相关函数:
std::string to_string(){
return std::to_string(type) + ", " + std::to_string(name) + ", " + std::to_string(part_number) + ", " + std::to_string(price);
}
我的 IDE 在我的 to_string() 函数中突出显示类型、名称、零件编号和价格,并出现上述错误。 如果在类中声明了变量,为什么它不能识别?
【问题讨论】:
-
你知道你试图在两个已经是
std::string的成员变量上调用std::to_string吗? -
好电话。感谢您指出这一点。