8

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

whelanevan6
  • 143
  • 1
  • 1
  • 4

4 Answers4

17

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;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
10

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.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
6

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();
}
SHR
  • 7,940
  • 9
  • 38
  • 57
3

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

Or, in C++20 you can use std::format.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93