C++ 连接字符串会导致"invalid operands of types ‘const char*’ and ‘const char"
C++ Concating strings result in "invalid operands of types ‘const char*’ and ‘const char"
我想连接两个字符串,但我遇到了一个错误,我不知道如何克服这个错误。
有没有办法将这个常量char*转换为char?我应该使用一些取消引用吗?
../src/main.cpp:38: error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’
make: *** [src/main.o] Error 1
然而,如果我试图用这种方式组成"底部"字符串,它会起作用:
bottom += "| ";
bottom += tmp[j];
bottom += " ";
这是代码。
#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;
}
此处:
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]) + " ";
由于定义了std::string
和char
的operator +
并返回std::string
,因此计算括号内的子表达式的结果将是std::string
,然后将其求和为字符串文字" "
,从而(再次(返回std::string
。
最后,整个表达式(std::string("| ") + tmp[j]) + " "
的结果在operator +=
的输入中给出。
看起来你的问题就在这一行:
bottom += "| " + tmp[j] " ";
您在tmp[j]
和" "
之间缺少一个+
。尝试将其更改为:
bottom += "| " + tmp[j] + " ";
编辑:
以上仍然会导致编译错误,而您所说的w/g++,以下将起作用,并产生最小的临时对象:
bottom += std::string("| ") + tmp[j] + " ";
看起来tmp[j]和"之间缺少一个+。行应读取
bottom += "| " + tmp[j] + " ";
问题是
tmp[j] + " "
这是不可能的,operator+
没有为char定义。
然而,operator+
是为字符串定义的。所以你可以做
bottom += "| "
请记住,编译将计算equals语句的整个右侧,然后在上使用string::operator+=
。
因此,编译器尝试评估"| " + tmp[j] + " "
。它看到的是一个c字符串、一个char和另一个c串。编译器不可能以任何的方式在中添加这些内容,这对你想要做的事情都有意义。
相反,你必须一段接一段地连接,这样编译器就知道在string
中查找运算符,该运算符被重载以连接字符串,或者你可以将c字符串转换为c++字符串,这样编译器就会知道使用c++字符串:
bottom += string("| ") + tmp[j] + string(" ");
在这个特定的例子中,您可以在" "
IIRC周围丢弃string()
,但这不是最好的方法
在任何情况下,一次附加一个段是告诉编译器您想要什么的最明确的方式,也是应该执行的方式。
bottom += "| " + tmp[j] " ";
这一行是你的问题,但不仅仅是缺少"+"。
您添加了"| "
和tmp[j]
,第一个是"const-char[3]",第二个是"char"。这些不会增加任何有用的东西,但编译器会将您的char转换为int,将const char[3]转换为const char*,并将它们添加在一起。然后,你试图向这个const char*添加一个"const char[2]",这时它知道它不理解它——向指针添加一个数组?这就是您的错误消息所报告的内容——它发现了这个"const char*"和一个"const char[2]",但它不知道如何将它们相加。
从逻辑上讲,您正试图创建一个读作"|?"的字符串,其中?已替换为tmp[j]字符。但是,您不能像现在这样使用字符串文字连接来实现这一点。一种可能的方法是使用sprintf将其放入字符串中,并将其添加到底部。第二种解决方案是分别添加它们。第三个解决方案是首先将tmp[j]转换为std::字符串,然后添加它-因为向std::string添加"+"是一个定义好的操作。
简而言之,您已经被一些C遗留问题所困扰,C++无法在不破坏C兼容性的情况下修复这些遗留问题。
带有sprintf的示例解决方案:
char buffer[5];
sprintf(buffer, "| %c ", tmp[j]);
bottom += buffer;
因为您想要添加指针和字符,而这样的操作是非法的。如果你不想在一行中完成,你可以这样尝试:
string bottom=";bottom=bottom+"|"+tmp[j]+";
这会很好用的。