Yasmine Takatart
Yasmine Takatart

Reputation: 23

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

I am getting the below error when I use the query to insert data in a database .

This is my code :

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;
    }
}

Do you have any ideas. Thank you in advance.

Upvotes: 0

Views: 731

Answers (4)

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12342

The problem is that you are not working with c++ strings but simple C string literal when doing:

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 )";

In that line the "INSERT..." decays to a const char * as will all the other C string constants. And const char * only has operator +(ptrdiff_t) that will advance the pointer.

What you can do is use C++ std::string literals like so and work with `std::string exclusively:

#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;

Note: You have to convert id_journal and any other variable you want to append to the string to std::string first.

Upvotes: 0

Alan
Alan

Reputation: 1

The problem is that you're adding the string literal

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

to an int named id_journal which results in a const char*.

Then you're trying to add this resulting const char* to the string literal "','" which is of type const char[4] but since there is no overloaded operator+ which takes a const char* and an const char array you end up with the mentioned error saying :

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

Upvotes: 1

Yasmine Takatart
Yasmine Takatart

Reputation: 23

Thank you I just had to convert the id_journal from int to string.

There is the solution and it works.


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;
    }
}

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409432

Like the error message says, you can't add pointers and arrays

The problematic part is:

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

Here the literal string "INSERT ..." will decay to a pointer (const char*) and then the value of id_journal is added. This result in the pointer &(("INSERT ...")[id_journal])). In other words, the value of id_journal is used as an array index instead of being converted to a string.

You then try to add this pointer to the literal string "','" which is really a constant array of four characters (including the string null-terminator).

There no usable + operator which can handle this.

The simplest solution is to turn at least one of the operands of the first addition to a std::string object. I suggest the integer variable id_journal since you can't concatenate strings with integers (there's no automatic conversion here):

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

This works because there is an overloaded + operator which takes a const char* on the left-hand side and a std::string on the right-hand side. Then once this is done, you have a std::string object which can be used for any further concatenations.

Upvotes: 2

Related Questions