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

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 3
    Use `std::to_string` or a sting stream. – chris Oct 03 '13 at 00:37
  • @chris, I tried that I get this error: error: no member named 'to_string' in namespace 'std' – user1471980 Oct 03 '13 at 00: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`: http://www.cplusplus.com/articles/D9j2Nwbp/ – CoderDake Oct 03 '13 at 00:42
  • @user1471980, It's C++11 and in ``. Older versions of GCC could have problems with it as well. – chris Oct 03 '13 at 00: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. – Hot Licks Oct 03 '13 at 00:55
  • boost::lexical_cast is also useful for this purpose – Slava Oct 03 '13 at 02:36

3 Answers3

14

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2
std::stringstream s;
s << "Manufacturer's Name: " << name
  << ", Model Name: " << model
  << ", Model Year: " << year
  << ", Miles: " << miles;

s.str();
dyp
  • 38,334
  • 13
  • 112
  • 177
Hei
  • 1,844
  • 3
  • 21
  • 35
  • 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](http://stackoverflow.com/help/formatting) – dyp Oct 03 '13 at 01:31
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
LihO
  • 41,190
  • 11
  • 99
  • 167