[英]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.
先感谢您。
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.此处文字字符串
"INSERT ..."
将衰减为指针 ( const char*
),然后添加id_journal
的值。 This result in the pointer &(("INSERT ...")[id_journal]))
.这导致指针
&(("INSERT ...")[id_journal]))
。 In other words, the value of id_journal
is used as an array index instead of being converted to a string.换句话说,
id_journal
的值被用作数组索引,而不是被转换为字符串。
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.最简单的解决方案是将第一个加法的至少一个操作数转换为
std::string
对象。 I suggest the integer variable id_journal
since you can't concatenate strings with integers (there's no automatic conversion here):我建议使用整数变量
id_journal
因为您不能将字符串与整数连接(这里没有自动转换):
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.这是有效的,因为有一个重载的
+
运算符,它在左侧采用const char*
,在右侧采用std::string
。 Then once this is done, you have a std::string
object which can be used for any further concatenations.然后一旦完成,您就有一个
std::string
对象,可用于任何进一步的连接。
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*
.到一个名为
id_journal
的int
,它产生一个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 :然后,您尝试将此生成的
const char*
添加到类型为const char[4]
的字符串文字"','"
但由于没有重载operator+
需要一个const char*
和一个const char
数组,因此您结束上面提到的错误说:
invalid operands of types ‘const char*’ and ‘const char [4]’ to binary ‘operator+’
Thank you I just had to convert the id_journal from int to string.谢谢,我只需要将 id_journal 从 int 转换为 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;
}
}
The problem is that you are not working with c++ strings but simple C string literal when doing:问题是您在执行以下操作时使用的不是 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 )";
In that line the "INSERT..."
decays to a const char *
as will all the other C string constants.在该行中,
"INSERT..."
衰减为const char *
,所有其他 C 字符串常量也一样。 And const char *
only has operator +(ptrdiff_t)
that will advance the pointer.并且
const char *
只有operator +(ptrdiff_t)
将推进指针。
What you can do is use C++ std::string literals like so and work with `std::string exclusively:您可以做的是像这样使用 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;
Note: You have to convert id_journal
and any other variable you want to append to the string to std::string
first.注意:您必须首先将
id_journal
和任何其他要附加到字符串的变量转换为std::string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.