3

I am trying to create a string that points to a file and am getting this error:

.../testApp.cpp:75: error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'

Here is the line in question:

    string path = "images/" + i + ".png";        

This seems like a fairly simple issue, yet it confounds me. I also included the string header:

#include <string>
using namespace std
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
Mike
  • 963
  • 4
  • 16
  • 38

6 Answers6

5

or boost::format:

std::string str = (boost::format("images/%d.png") % i).str();

boost::format(FORMATTED_STIRNG) % .. %.. %.. is for formatted string processing, see wiki. this function gives you back a special boost format which you need to cast to std::string using its .str() member function.

guinny
  • 1,522
  • 10
  • 19
4

You need to convert i to a std::string:

string path = "images/" + boost::lexical_cast<string>(i) + ".png";

For other approaches to converting an int to a std::string see Append an int to a std::string

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
3

Use a stringstream instead, std::string doesn't support off-the-rack formatting for integers.

std::stringstream ss;
ss << "images/" << i << ".png";
std::string path = ss.str();
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You are trying to concatenate string literals as if they are std::string objects. They are not. In C++ string literals are of type const char[], not std::string.

To join two string literals, place them next to each other with no operator:

const char* cat = "Hello " "world";

To join two std::string objects, use operator+(std::string, std::string):

std::string hello("hello ");
std::string world("world\n");
std::sting cat = hello + world;

There is also an operator+ to join a string literal and a std::string:

std::string hello("hello ");
std::string cat = hello + "world\n";

There is no operator+ that takes std::string and int.

A solution to your problem is to use std::stringstream, which takes any operator<< that std::cout can take:

std::stringstream spath;
spath << "images/" << i << ".png";
std::string path = spath.str();
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • _technicially_ string literals will concatenate with std::string, and with each other (although with a different syntax). You should reword part 1. – Mooing Duck May 29 '12 at 14:22
1

With C++11 we get a set of to_string functions that can help converting built in numeric types to std::string. You can use that in your conversion:

string path = "images/" + to_string(i) + ".png";         
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

To quote all of the other answers, yes, std::string doesn't have built in support for appending integers. However, you can add an operator to do just that:

template<typename T>
std::string operator +(const std::string &param1, const T& param2)
{
    std::stringstream ss;
    ss << param1 << param2;

    return ss.str();
}

template <typename T>
std::string operator +(const T& param1, const std::string& param2) {
    std::stringstream ss;
    ss << param1 << param2;

    return ss.str();
}

template <typename T>
std::string& operator +=(std::string& param1, const T& param2) 
{
    std::stringstream ss;
    ss << param1 << param2;

    param1 = ss.str();
    return param1;
}

The only real disadvantage to this is you must first cast one of the literals to a string, like this:

string s = string("Hello ") + 10 + "World!";
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • I'd want to test this thoroughly before using it, this could potentially cause certail well defined behavior to suddenly get an ambiguity error. – Mooing Duck May 29 '12 at 14:21