Caladbolgll
Caladbolgll

Reputation: 87

string concatenation in c++

Previously, I was using append function to concatenate strings. However, since doing so requires multiple lines of unnecessary codes, I wanted to try out '+' operator instead. Unfortunately, it didn't go well...

bool Grid::is_available(int x, int y) const
{
    if (x < 0 || x >= dim[1] || y < 0 || y >= dim[0])
        throw std::invalid_argument("is_available(" + x + ", " + y + "): Invalid coordinate input.");
    return occupancy[x][y] == AVAILABLE;
}

The error that I got was "'+': cannot add two pointers" with the code C2110. All the solutions for this problem said to concatenate one on each line. Are there actually no way to concatenate multiple strings in C++ in one line? I had no problem with this in C# before.

Upvotes: 2

Views: 315

Answers (1)

Galik
Galik

Reputation: 48645

You can use std::to_string() to convert your integers:

bool Grid::is_available(int x, int y) const
{
    if (x < 0 || x >= dim[1] || y < 0 || y >= dim[0])
        throw std::invalid_argument(
            "is_available(" + std::to_string(x) + ", "
                + std::to_string(y) + "): Invalid coordinate input.");
    return occupancy[x][y] == AVAILABLE;
}

Upvotes: 2

Related Questions