简体   繁体   English

在c ++ struct上创建自己的toString()方法

[英]Making my own toString() method on c++ struct

I'm used to oveerriding the Java toString() method on my own objects in classes, but I'm not sure where I'm going wrong with the following code: 我习惯在类中自己的对象上运行Java toString()方法,但我不确定我在下面的代码出错了:

    struct Student {
    std::string name;
    int age;
    double finalGrade;

    std::string toString() {
        return "Name: " + name + "\n Age: " + age + "\n Final Grade: " + finalGrade;
    }
};

I'm only beginning to learn c++ so any advice would be appreciated 我只是开始学习c ++所以任何建议都会受到赞赏

You can't add anything you want to a std::string like you can to a Java String . 您不能添加你想要一个什么std::string像您可以将Java String Notably, most objects are not expected to have a toString member function. 值得注意的是,大多数对象不应具有toString成员函数。 However, the standard library provides std::to_string which allow you to convert numeric values to an std::string . 但是,标准库提供了std::to_string ,它允许您将数值转换为std::string For example you could wrap the numeric values with a std::to_string to fix your function : 例如,您可以使用std::to_string包装数值来修复您的函数:

#include <string>

struct Student {
    std::string name;
    int age;
    double finalGrade;

    std::string toString() {
        return "Name: " + 
            name + 
            "\n Age: " + 
            std::to_string(age) + 
            "\n Final Grade: " + 
            std::to_string(finalGrade);
    }
};

Edit : Though this answer explains why what you tried doesn't work, the other answer's solution is the preferred approach. 编辑:虽然这个答案解释了为什么你尝试的不起作用,但另一个答案的解决方案是首选方法。

In contrast to java, C++ does not offer a predefined "toString"-method that is called implicitly whenever a string representation of an object is requested. 与java相比,C ++不提供预定义的“toString”方法,只要请求对象的字符串表示,就会隐式调用该方法。 So your toString -method will have to be called explicitly then. 因此,必须明确调用你的toString -method。

In C++, however, something similar is available by overriding the operator << for streams. 但是,在C ++中,通过覆盖operator << for streams,可以获得类似的东西。 Thereby, you can directly "send" the object contents to a stream (without the need to store everything in an intermediate string object). 因此,您可以直接将对象内容“发送”到流中(无需将所有内容存储在中间字符串对象中)。 And you can use the same code to populate a string to be returned by a toString method, too: 并且您也可以使用相同的代码来填充由toString方法返回的字符串:

struct Student {
    std::string name;
    int age;
    double finalGrade;

    std::string toString() const;
};

ostream& operator << (ostream &os, const Student &s) {
    return (os << "Name: " << s.name << "\n Age: " << s.age << "\n Final Grade: " << s.finalGrade  << std::endl);
}

std::string Student::toString() const {
    stringstream ss;
    ss << (*this);
    return ss.str();
}

int main() {

    Student stud { "john baker", 25, 1.2 };
    std::cout << "stud directly: " << stud << endl;
    std::string studStr = stud.toString();
    std::cout << "stud toString:" << studStr << endl;
}

You cant add int to std::string because the std::string operator+ was not overloaded to int. 你不能将int添加到std :: string,因为std::string operator+没有重载到int。

The best solution is to use string stream : 最好的解决方案是使用字符串流:

#include <sstream>

std::string toString() {
    std::ostringstream strout;
    strout<< "Name: " << name << "\n Age: " << age << "\n Final Grade: " << finalGrade;
    return strout.str();
}

You cannot just add an int or a double to an std::string . 你不能只是在std::string添加intdouble Use std::to_string to convert them first. 使用std :: to_string首先转换它们。 This should work fine: 这应该工作正常:

std::string toString() {
    return "Name: " + name + "\n Age: " + std::to_string(age) + "\n Final Grade: " + std::to_string(finalGrade);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM