Mike
Mike

Reputation: 963

Error when concatenating strings (C++)

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

Upvotes: 3

Views: 2698

Answers (6)

guinny
guinny

Reputation: 1532

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.

Upvotes: 5

Robᵩ
Robᵩ

Reputation: 168616

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

Upvotes: 2

Bo Persson
Bo Persson

Reputation: 92211

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

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

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

Upvotes: 0

hmjd
hmjd

Reputation: 121961

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

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258548

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

Upvotes: 3

Related Questions