类型 'const char*' 和 const char[4]' 到二进制 'operator+' 的无效操作数

invalid operands of types 'const char*' and const char[4]' to binary 'operator+'

当我使用查询向数据库中插入数据时出现以下错误。

这是我的代码:

void Journal::insert_info()
{
    //Variables
    int id_journal= getId_journal();
    string nom_journal=getNom_journal();

//Here is where the error 
    string insert_query = "INSERT INTO `info_journal`(`id_journal`, `nom_journal`, `date`, `id_fournisseur`, `id_atm`, `state`, `state_parse_journal`, `terminal`, `id_utilisateur`) VALUES ('"+id_journal+"','"+nom_journal+"',NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL )";
    //int qstate = mysql_real_query(conn,insert_query.c_str(), strlen(insert_query.c_str()));
    query_state=mysql_query(conn, insert_query.c_str());
    if(!query_state)
    {

        cout << "Query Execution Problem " << mysql_errno(conn) << endl;
    }
    else
    {
         cout << endl << "success" << endl;
    }
}

你有什么想法吗? 提前谢谢你。

如错误信息所说,不能添加指针和数组

有问题的部分是:

"INSERT ..." + id_journal + "','"

这里的文字串"INSERT ..."会衰减为一个指针(const char*)然后加上id_journal的值。这导致指针 &(("INSERT ...")[id_journal]))。换句话说,id_journal的值被用作数组索引,而不是被转换为字符串。

然后尝试将此指针添加到文字字符串 "','",它实际上是一个包含四个字符的常量数组(包括字符串 null-terminator)。

没有可用的 + 运算符可以处理这个问题。

最简单的解决方案是将第一个加法运算的至少一个操作数转为 std::string 对象。我建议整数变量 id_journal 因为你不能将字符串与整数连接(这里没有自动转换):

string insert_query = "INSERT ...VALUES ('" + std::to_string(id_journal) + "','" + ...;

这是有效的,因为有一个重载的 + 运算符,它在 left-hand 端接受一个 const char*,在 right-hand 端接受一个 std::string。然后一旦完成,您就有了一个 std::string 对象,可用于任何进一步的连接。

问题是您要添加字符串文字

"INSERT INTO `info_journal`(`id_journal`, `nom_journal`, `date`, `id_fournisseur`, `id_atm`, `state`, `state_parse_journal`, `terminal`, `id_utilisateur`) VALUES ('"

到名为 id_journalint,结果是 const char*.

然后您尝试将此结果 const char* 添加到 const char[4] 类型的字符串文字 "','" 中,但由于没有重载 operator+一个 const char* 和一个 const char 数组,你最终得到提到的错误:

invalid operands of types ‘const char*’ and ‘const char [4]’ to binary ‘operator+’

谢谢,我只需要将 id_journal 从 int 转换为 string。

有解决方案,而且有效。


void Journal::insert_info()
{
    Db_response::ConnectionFunction();
    //Variables
    int id_journal= getId_journal();
    //string nom_journal=getNom_journal();
    string nom_journal = find_nom_journal();
    string s_id_journal(to_string(id_journal));

    string insert_query = "INSERT INTO `info_journal`(`id_journal`, `nom_journal`, `date`, `id_fournisseur`, `id_atm`, `state`, `state_parse_journal`, `terminal`, `id_utilisateur`) VALUES ('"+s_id_journal+"','"+nom_journal+"',' '  ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' )";
    //int qstate = mysql_real_query(conn,insert_query.c_str(), strlen(insert_query.c_str()));
    query_state=mysql_query(conn, insert_query.c_str());
    if(!query_state)
    {

        cout << "Query Execution Problem " << mysql_errno(conn) << endl;
    }
    else
    {
         cout << endl << "success" << endl;
    }
}

问题是您在执行以下操作时使用的不是 C++ 字符串,而是简单的 C 字符串文字:

string insert_query = "INSERT INTO `info_journal`(`id_journal`, `nom_journal`, `date`, `id_fournisseur`, `id_atm`, `state`, `state_parse_journal`, `terminal`, `id_utilisateur`) VALUES ('"
                      + id_journal + "','" + nom_journal
                      + "',NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL )";

在那一行中,"INSERT..." 衰减为 const char *,所有其他 C 字符串常量也是如此。而const char *只有operator +(ptrdiff_t)会让指针前进。

你可以做的是像这样使用 C++ std::string 文字并专门使用 `std::string:

#include <string>
using namespace std::literals;

int id_journal = 42;
std::string nom_journal = "blub";

std::string insert_query = "INSERT INTO `info_journal`(`id_journal`, `nom_journal`, `date`, `id_fournisseur`, `id_atm`, `state`, `state_parse_journal`, `terminal`, `id_utilisateur`) VALUES ('"s
                           + std::string::to_string(id_journal)
                           + "','"s
                           + nom_journal
                           + "',NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL )"s;

注意:您必须先将 id_journal 和任何其他要附加到字符串的变量转换为 std::string