如何修复C++中的"错误:从'int'到'const char*'[-fallowive]的无效转换?

How to fix 'error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]' in C++?

本文关键字:char -fallowive 转换 无效 const int 中的 C++ 何修复 错误      更新时间:2023-10-16

我尝试连接mysql和c 。这是试图将输入值放入数据库的过程。当G 编译时发生错误。

我正在使用C 98在Visual Studio 2017和CentOS7中进行编码。使用的mySQL连接器是mysql ver.8.0。

#include "[~ PATH]/mysql++/include/mysql++/cmdline.h"
#include "[~ PATH]/mysql++/include/mysql++/mysql++.h"
#include "[~ PATH]/mysql++/examples/printdata.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
        char buffer[256];
        string name;
        string id;
        string password;
        string department;
        cout << ">> Sign Up <<n" << endl;
        cout << "1.Name: "; cin >> name;
        cout << "2.ID: "; cin >> id;
        cout << "3.PASSWORD: "; cin >> pw;
        cout << "4.Department: "; cin >> dpt;
        mysqlpp::Connection conn(false);
        if (conn.connect("member_list", "localhost", "user", "password1234"))
        {
                **mysqlpp::Query query 
                 = conn.query(sprintf(buffer,"INSERT INTO signup_member (name,id,password,department) 
                   VALUES ('%s','%s','%s','%s')",name.c_str(),id.c_str(),password.c_str(),department.c_str()));**
                if (mysqlpp::StoreQueryResult res = query.store()) {
                        cout << "Sign up SUCCESS!" << endl;
                }
                else {
                        cerr << "Sign up FAIL. Try again." << 'n' << query.error() << endl;
                        return 1;
                }
                return 0;
        }
        else {
                cerr << "DB connection failed: " << conn.error() << endl;
        }
}

当我搜索时,它说要使用" sprintf"将值保存在缓冲区中,然后将其保存在DB中。我希望它运行良好,但事实并非如此。 sign_up.cpp:30:175: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]

sprintf返回代表

的int

成功,返回了书面字符的总数。这 计数不会自动包含额外的空字符 附加在字符串的末尾。失败,负数为 返回。

&amp;不是您所期望的那样写数据的字符阵列,所以基本上应该这样做:

sprintf(buffer,"INSERT INTO signup_member (name,id,password,department) 
                   VALUES ('%s','%s','%s','%s')",name.c_str(),id.c_str(),password.c_str(),department.c_str());
mysqlpp::Query query 
                 = conn.query(buffer);