C ++ Concrete strings lead to "invalid operands of type const char * and 'const char"

I want to concatenate two lines, but I get an error that I do not understand how to overcome this error.

Is there any way to convert this const char * to char? Should I use dereferencing?

../src/main.cpp:38: error: invalid operands of types ‘const char*’ andconst char [2]’ to binary ‘operator+’
make: *** [src/main.o] Error 1

However, if I try to create a “bottom” line 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;
}
+5
source share
7 answers

Here:

bottom += "| " + tmp[j] " ";

char char. ( ). , + tmp[j], ( , , operator + ):

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.

, :

bottom += std::string("| ") + tmp[j] + " ";

:

(std::string("| ") + tmp[j]) + " ";

operator + a std::string a char std::string, std::string, literal " ", () a std::string.

, (std::string("| ") + tmp[j]) + " " operator +=.

+7

, :

bottom += "| " + tmp[j] " ";

+ tmp[j] " ". :

bottom += "| " + tmp[j] + " ";

Edit:

- , , w/g++, :

bottom += std::string("| ") + tmp[j] + " ";
+3

, + tmp [j] "".

bottom += "| " + tmp[j] + " ";
0

tmp[j] + " "

operator+ char.

operator+ . ,

bottom += "| "
0

, equals, string::operator+=.

, "| " + tmp[j] + " ". c-, char c-. , , , .

, , string , , , c- ++, , ++:

bottom += string("| ") + tmp[j] + string(" ");

string() " " IIRC, .

- , , .

0
            bottom += "| " + tmp[j] " ";

, "+".

"| " tmp[j], - "const char [3]", - "char". , char int, const char [3] const char * . "const char [2]" const char *, , - ? - "const char *" "const char [2]", , .

, , "|?", ? tmp [j]. , , . - sprintf, . , . , tmp [j] std::string - "+" std::string , .

, C, ++ , C.

sprintf:

char buffer[5];
sprintf(buffer, "| %c ", tmp[j]);
bottom += buffer;
0

because you want to add a pointer and char, and such an operation is illegal. if you do not want to do this on one line, you can try this as follows:

string bottom = ""; bottom = bottom + "|" + tmp [j] + "";

this will work fine.

0
source

All Articles