我正在尝试创建一个列数可变的表。 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/