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++.
std::to_stringor a sting stream.to_stringfor some reason. Take a look at stringstreams for the conversion frominttostring: cplusplus.com/articles/D9j2Nwbp<string>. Older versions of GCC could have problems with it as well.