大家好,我是新来的。我刚开始用C ++编程,我似乎并没有让我的getter方法正常工作。这是我的代码:
#pragma once
class Person
{
private:
std::string name;
int age;
double weight;
double height;
public:
Person();
Person(std::string _name, int _age, double _weight, double _height)
: name{ _name }, age{ _age }, weight{ _weight }, height{ _height }
{}
~Person();
std::string get_name() { return name; }
int get_age() { return age; }
double get_weight() { return weight; }
double get_height() { return height; }
};
当我尝试调用get_name函数时出现错误,但其他getter方法工作正常。
我打电话给这样的方法:
Person *guy = new Person("Chad", 30, 70.0, 185.0);
cout << "Name = " << guy->get_name() << " Age = " << guy->get_age()<< "Weight = " << guy->get_weight() << "Height = " << guy->get_height() << endl;
但是给我以下错误:
错误(有效)E0349无操作员&#34;&lt;&lt;&#;匹配这些 操作数Project2 c:\ Users \ Abdullah \ source \ repos \ Project2 \ Project2 \ Source.cpp 37
擦除get_name()方法后,错误消失。有谁知道为什么会这样?