我正在尝试为c ++设置mysql连接器。当我尝试编译此代码时,出现此错误/home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous
请解释如何解决这个问题,因为我很难过。
CODE
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
// #include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql::mysql;
int main(int argc, char const *argv[])
{
/* code */
cout << "STarting the MYSQL STUFF \n";
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.1.2:3306", "root", "");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("insert into cTest (text) value ('testing and stuff')");
cout << "inserted stuff\n";
res = stmt->executeQuery("SELECT 'cTest' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: \n";
/* Access column data by alias or column name */
cout << res->getString("_message");
cout << "\t... MySQL says it again: \n";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1);
}
return 0;
}
ERROR
/home/cjueden/programming projects/mysqlConnect/main.cpp||In function ‘int main(int, const char**)’:|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|note: candidates are:|
/usr/include/cppconn/driver.h|62|note: sql::Driver* get_driver_instance()|
/usr/include/mysql_driver.h|86|note: sql::mysql::MySQL_Driver* sql::mysql::get_driver_instance()
答案 0 :(得分:0)
看起来driver.h中的get_driver_instance()版本在全局命名空间中,所以这里有一个修复:
driver = ::get_driver_instance();
编译器不知道要使用哪两个函数。根据你对返回值的处理,我猜你打算在driver.h中使用它。
在全局命名空间中使用东西(如此函数)时,预加双冒号(::)是一种更明确的方式。它解决了歧义。
另一个修复方法是删除前面的“using namespace sql :: mysql;”。