错误:'sql'没有命名类型 sql :: Connection connect_mysql(); 有没有办法可以从这个函数返回连接对象? 我知道在C中返回连接对象但在C ++中没有发生 编辑版本:connect_mysql()的返回类型已更改为sql :: Connection *。早些时候它是sql :: Connection。
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include<mysql/mysql.h>
#include<cppconn/driver.h>
#include<cppconn/exception.h>
#include<cppconn/resultset.h>
#include<cppconn/statement.h>
sql::Connection * connect_mysql();
int main()
{
sql::Connection *con;
con = connect_mysql();
return 0;
}
sql::Connection * connect_mysql()
{
cout << "CONNECTING DATABASE..............."<<endl;
try{
cout<<"inside try while connecting to mysql"<<endl;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
con = driver->connect("localhost","root","Aricent@123");
con->setSchema( "COE" );
stmt = con->createStatement();
res = stmt->executeQuery( "show tables" );
while( res->next() )
{
cout<<"MYSQL replies:"<<endl;
cout<<res->getInt(1);
cout<<res->getString(1)<<endl;
}
}
catch( exception e )
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "# ERR: ";
cout << " (MySQL error code: ";
cout << ", SQLState: ";
}
return con;
}
答案 0 :(得分:1)
这不是一个对象,它是一个指针。试试这个
sql::Connection* connect_mysql()
{
...
}
另一个错误是缺少connect_mysql
的原型。你应该有这样的东西
sql::Connection* connect_mysql(); // prototype
int main()
{
sql::Connection *con;
con = connect_mysql();
...
但令我困惑的是,您的错误消息是它引用了行sql::Connection* connect_mysql()
但显然在行sql::Connection *con;
中使用相同的类型是可以的。如果您有任何其他错误消息或警告,请同时发布。
不应该声明为了获得有关编译器错误消息的帮助,您应该发布确切的代码,确切的错误消息以及所有这些消息。
我不是mysql的专家,但我想知道你是否需要头文件#include <cppconn/connection.h>