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:

    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

6

4 Answers

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. So your toString-method will have to be called explicitly then.

In C++, however, something similar is available by overriding the 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 toStringmethod, too:

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;
}
1

You can't add anything you want to a std::string like you can to a Java String. Notably, most objects are not expected to have a toString member function. However, the standard library provides std::to_string which allow you to convert numeric values to an std::string. For example you could wrap the numeric values with a std::to_string to fix your function :

#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.

0

You cant add int to std::string because the std::string operator+ was not overloaded to 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();
}
1

You cannot just add an int or a double to an std::string. Use std::to_string to convert them first. This should work fine:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest