3

I am trying to concatenate strings and integers as follows:

#include "Truck.h" #include <string> #include <iostream> using namespace std; Truck::Truck (string n, string m, int y) { name = n; model = m; year = y; miles = 0; } string Truck :: toString() { string truckString = "Manufacturer's Name: " + name + ", Model Name: " + model + ", Model Year: " + year ", Miles: " + miles; return truckString; } 

I am getting this error:

error: invalid operands to binary expression ('basic_string<char, std::char_traits<char>, std::allocator<char> >' and 'int') string truckString = "Manufacturer's Name: " + name + ", Model Name: " + model + ", Model Year: " + year ", Miles... 

Any ideas what I might be doing wrong? I am new to C++.

6
  • 3
    Use std::to_string or a sting stream. Commented Oct 3, 2013 at 0:37
  • @chris, I tried that I get this error: error: no member named 'to_string' in namespace 'std' Commented Oct 3, 2013 at 0:40
  • I was running into this problem earlier today and found out that depending on your compiler set up, you might not have access to to_string for some reason. Take a look at stringstreams for the conversion from int to string: cplusplus.com/articles/D9j2Nwbp Commented Oct 3, 2013 at 0:42
  • @user1471980, It's C++11 and in <string>. Older versions of GCC could have problems with it as well. Commented Oct 3, 2013 at 0:46
  • You don't (or shouldn't) concatenate strings and integers. You can concatenate strings with other strings, and some of those strings may be the character representation of integers, but they are not integers. Many languages (including Java and C++) help you with this by implicitly converting integers into their string representations when certain operators are used. Commented Oct 3, 2013 at 0:55

3 Answers 3

15

In C++03, as other people have mentioned, you can use the ostringstream type, defined in <sstream>:

std::ostringstream stream; stream << "Mixed data, like this int: " << 137; std::string result = stream.str(); 

In C++11, you can use the std::to_string function, which is conveniently declared in <string>:

std::string result = "Adding things is this much fun: " + std::to_string(137); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

Use std::ostringstream:

std::string name, model; int year, miles; ... std::ostringstream os; os << "Manufacturer's Name: " << name << ", Model Name: " << model << ", Model Year: " << year << ", Miles: " << miles; std::cout << os.str(); // <-- .str() to obtain a std::string object 

Comments

2
std::stringstream s; s << "Manufacturer's Name: " << name << ", Model Name: " << model << ", Model Year: " << year << ", Miles: " << miles; s.str(); 

1 Comment

Please make sure your code is indented with four spaces, or press CTRL+K to indent it automatically. Otherwise, it'll be interpreted as plain text. See formatting help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.