C++ Person 类 - 无法输出值和修改元素

标签 c++ function class object

我正在尝试创建一个具有以下值的 Person 类:字符串名称、整数年龄、整数高度、整数体重。我应该实例化一个 Person 对象,给它赋值,然后输出这些值。另外,我应该有一个 ModifyPerson 函数,它接受一个 Person 对象作为参数,并更改名称成员变量并输出它。任何帮助将不胜感激。

编辑:我目前在代码中遇到的唯一问题是正确输出 person1 的成员值

EDIT2:已修复!谢谢斑马鱼

    #include <iostream>
    #include <string>
    using namespace std;

    class Person 
    {
        string name;
        int age, height, weight;
    public:
        void set_values(string, int, int, int);
        string ModifyPerson();
        void coutDetails() const;
    }person1;

    void Person::set_values(string a, int b, int c, int d) 
    {
        name = "Rob";
        age = 19;
        height = 71;
        weight = 180;
    }

    string Person::ModifyPerson()
    {
        string name = "Robert";
        return name;
    }

    void Person::coutDetails() const
    {
        cout << "\nName: " << name << "\nAge: " << age << "\nHeight: " <<         
        height << "\nWeight: " << weight;
    }

    std::ostream& operator<<(const std::ostream&, const Person& person1)
    {
        person1.coutDetails();
        return std::cout;
    }

    int main()
    {
        Person person1;
        person1.set_values("Rob", 19, 71, 180);
        cout << person1 << endl;
        cout << person1.ModifyPerson() << endl;
        return 0;
    }

最佳答案

此行失败的原因:

cout << person1 << endl;

错误信息告诉我们:

'<<': no operator found which takes a right-hand operand of type 'Person' (or there is no acceptable conversion)

std::cout 是一个 std::ostream 对象,没有运算符 << 获取 Person 对象,除非您专门创建一个。

第二个cout行:

cout << person1.ModifyPerson() << endl;

由于不同的原因而失败。您的类 Person 中肯定有方法 ModifyPerson(),但是它有两个问题:

1)返回类型不同

2) 您已将 ModifyPerson() 定义为全局函数,而不是成员类方法。

因此您需要确保返回类型相同,并且当您定义 ModifyPerson() 方法时,您应该事先放置类名,如下所示:

string Person::ModifyPerson(){}

我觉得您已经知道这一点,因为您在方法 set_values 前面加上了 Person::

代码应该是这样的:

#include <iostream>
#include <string>
using namespace std;

class Person
{
    string name;
    int age, height, weight;

public:
    void set_values(string, int, int, int);
    string ModifyPerson();
}person1;

void Person::set_values(string a, int b, int c, int d)
{
    name = "Rob";
    age = 19;
    height = 71;
    weight = 180;
}

string Person::ModifyPerson()
{
    string name = "Robert";
    return name;
}

int main()
{
    Person person1;
    person1.set_values("Rob", 19, 71, 180);
    //cout << person1 << endl;
    cout << person1.ModifyPerson() << endl;
    return 0;
}

此外,如果您希望能够编写 cout << person1,您将需要编写一个将 std::ostream 作为第一个参数的全局函数,例如:

std::ostream& operator<<(const std::ostream&, const Person& person)
{
    person.coutDetails();
    return std::cout;
}

Person 的 coutDetails() 方法如下所示:

void Person::coutDetails() const
{
    cout << "\nName: " << name << "\nAge: " << age << "\nHeight:" <<
        height << "\nWeight: " << weight;
}

编辑:我注意到自从我写了这个答案后你改变了你的 ModifyPerson 函数。这部分内容仅适用于您的原始帖子。

关于C++ Person 类 - 无法输出值和修改元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49357151/

相关文章:

c++ - 如何在类中将 char 作为函数参数传递?

java - 使用未经检查或不安全的操作,使用 -Xlint 重新编译

c++ - '&' 需要 &std::unique_ptr<>.get 上的左值

javascript 函数没有按预期工作

jquery - 迭代无序列表时跳过第一个列表项。 jQuery

Python 3.x 短内存数字猜测(编程/数学)

C# 类类型转换

c++ - 在 Vista 上保持 cmd.exe 打开

c++ - c++中分离头文件及其逻辑

Boost.Fusion 中的 C++ 可变参数宏?