Reputation: 121
I want to concat two strings but i get error that i don't understand how to overcome this error.
Is there some way to convert this const char* to char? Should i use some dereferencing?
../src/main.cpp:38: error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’
make: *** [src/main.o] Error 1
However, if i try to make up "bottom" string this way, it works:
bottom += "| ";
bottom += tmp[j];
bottom += " ";
Here is the code.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <sstream>
int main(int argc, char* argv[]) {
ifstream file("file.txt");
vector<string> mapa;
string line, top, bottom;
while(getline(file,line)){
mapa.push_back(line);
}
string tmp;
for(int i = 0; i < mapa.size(); i++)
{
tmp = mapa[i];
for(int j = 0; j < tmp.size(); j++)
{
if(tmp[j] != ' ')
{
top += "+---";
bottom += "| " + tmp[j] + " ";
} else {
}
}
cout << top << endl;
cout << bottom << endl;
}
return 0;
}
Upvotes: 6
Views: 12924
Reputation: 725
because you want to add a pointer and a char, and such a operation is illegal. if you don't want to make it in a single line,you can try it like this:
string bottom = ""; bottom = bottom + "| " + tmp[j] + " ";
this will work fine.
Upvotes: 0
Reputation: 4590
Looks to be that your problem is on this line:
bottom += "| " + tmp[j] " ";
You're missing a +
between tmp[j]
and " "
. Try changing it to:
bottom += "| " + tmp[j] + " ";
Edit:
The above will still cause a compilation error, and the one you stated w/ g++, the following will work and result in minimal temporary objects:
bottom += std::string("| ") + tmp[j] + " ";
Upvotes: 3
Reputation: 7302
bottom += "| " + tmp[j] " ";
This line is your problem, but not just for the missing '+'.
You're adding "| "
and tmp[j]
, the first is a "const char[3]" and the second is a "char". These don't add to anything useful, but the compiler will convert your char to an int, the const char[3] to a const char * and add them together. Then you try to add a "const char [2]" to this const char *, and at that point it knows it doesn't understand it - adding an array to a pointer? This is what your error message is reporting - it's finding this "const char *" and a "const char [2]" which it can't figure out how to add together.
Logically, you're trying to create a string that reads "| ? " where the ? is replaced with your tmp[j] character. You can't do that with string literal concatenation as you're doing here now though. One possible way is to use sprintf to put this in a string and to add that to bottom. A second solution is to add them separately each. A third solution is to convert tmp[j] to an std::string first and to add that - as adding with a "+" to a std::string is a defined operation that works.
In short, you've been bitten by some C legacy that C++ couldn't fix without breaking C compatibility.
Example solution with sprintf:
char buffer[5];
sprintf(buffer, "| %c ", tmp[j]);
bottom += buffer;
Upvotes: 0
Reputation: 126562
Here:
bottom += "| " + tmp[j] " ";
You are trying to sum a char
and a pointer to char
. That won't work (it won't result in the concatenation of the character and the pointed string literal). And the same is true if you add a +
sign after tmp[j]
, because that will still be evaluated as (extra parentheses added to stress the fact that operator +
associates to the left):
bottom += ("| " + tmp[j]) + " "; // ERROR!
// ^^^^^^^^^^^^^
// This is still summing a character and a pointer,
// and the result will be added to another pointer,
// which is illegal.
If you want to have everything in one line, just do:
bottom += std::string("| ") + tmp[j] + " ";
Now, the above expression on the right side of the assignment will be evaluated as:
(std::string("| ") + tmp[j]) + " ";
Since operator +
for a std::string
and a char
is defined and returns an std::string
, the result of evaluating the sub-expression within parentheses will be an std::string
, which is then summed to the string literal " "
, returning (again) an std::string
.
Eventually, the result of the whole expression (std::string("| ") + tmp[j]) + " "
is given in input to operator +=
.
Upvotes: 7
Reputation: 4039
Keep in mind that the compile will evaluate the whole right side of the equals statement, and then use string::operator+=
on that.
So, the compiler tries to evaluate "| " + tmp[j] + " "
. What it sees is a c-string, a char, and another c-string. There's no way that the compiler can add these in any way that makes sense for what you're trying to do.
Instead, you have to concatenate piece by piece, so that the compiler knows to look in string
for the operator, which is overloaded to concatenate the strings, OR you may convert the c-strings into c++ strings so that the compiler knows to use c++ strings:
bottom += string("| ") + tmp[j] + string(" ");
In this specific instance, you could get away with dropping the string()
around " "
IIRC, but that's not the nicest way to do it.
In any case, appending one segment at a time is the most unambiguous way to tell the compiler exactly what you want, and is the way you should go about it.
Upvotes: 0
Reputation:
the problem is
tmp[j] + " "
This is not possible operator+
is not defined for char.
However operator+
is defined for string. So you are allowed to do
bottom += "| "
Upvotes: 0
Reputation: 765
It looks like you're missing a + between tmp[j] and " ". Line should read
bottom += "| " + tmp[j] + " ";
Upvotes: 0