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
1
  • What is the type of i? Commented May 29, 2012 at 13:27

6 Answers 6

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.

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

2 Comments

sorry, I considered the syntax to be quite obvious. but I added explanation now.
Undownvote-upvoted. Try to do this for your future posts as well :)
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

Comments

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

Comments

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

1 Comment

technicially string literals will concatenate with std::string, and with each other (although with a different syntax). You should reword part 1.
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";         

Comments

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!";

1 Comment

I'd want to test this thoroughly before using it, this could potentially cause certail well defined behavior to suddenly get an ambiguity error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.