c++ - 错误 : invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’

标签 c++ sql for-loop create-table vertica

我正在尝试创建一个列数可变的表。 YH(i, Y1, Y2 ....Yd)

所以我在查询中创建了一个 for 循环。但它显示以下错误 -

  error: invalid operands of types ‘const char*’ and ‘const char [7]’ to
  binary ‘operator+’
     for(int l=1;l<=d;l++) {commandline+=", Y"+ l +" real ";}

主要代码如下-

string commandline;
commandline = "DROP TABLE YH";
if(SQL_SUCCESS != SQLExecDirect(hdlStmt, (SQLCHAR*)(commandline.c_str()), SQL_NTS))
{
    cout<<"The drop YH table is unsuccessful."<<endl;
}

commandline = "CREATE TABLE YH"
        "(i int primary key ";
for(int l=1;l<=d;l++) {
    commandline+=", Y"+l+" real ";
}
commandline+=" ) ";
if(SQL_SUCCESS != SQLExecDirect(hdlStmt, (SQLCHAR*)(commandline.c_str()), SQL_NTS))
{
    cout<<"The create table sql command hasn't been executed successfully."<<endl;
}

我尝试了以下 -

for(int l=1;l<=d;l++) {commandline+=", Y" l " real ";}

for(int l=1;l<=d;l++) {commandline+=", Y"+std::string(l)+" real ";}

它们似乎都不起作用。

最佳答案

您不能使用 + 将整数连接到字符串。当你写

", Y" + l

它将 l 添加到指向字符串文字的指针,然后返回另一个指针。然后,当您执行 + "real" 时,它会尝试将指针添加到该数组,但是 + 运算符没有这样的重载。 + 只能在至少一个参数是 std::string 时用于连接。

std::string(l) 也不起作用。这不是您获得数字的字符串表示形式的方式。您想要的函数是 std::to_string(l)

commandline += ", Y" + std::to_string(l) + " real ";

关于c++ - 错误 : invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46357198/

相关文章:

c++ - QInputDialog.getItem() 获取项目索引

php - 关系型 MySQL PHP

c++ - 从 MAT 文件中读取 C 应用程序中的自定义类

c++ - 将动态数组传递给其他函数的正确方法

c++ - 无法打开存在的二进制文件

sql - 如何在mongodb上检索空查找条目?

sql - 将另一个表中的数据放入检查约束

python - 如何在 networkx 中高效地生成多个具有随机边权重的随机图

c++ - 循环抛出 "parser stack overflow, program too complex"的编译时间

java - 第 (N-2) 和 (N-1) 的第 N 个输出和