C++ c++&引用;在使用MySQL C API生成的共享库时,未在此作用域错误中声明
我生成了一个共享库,它封装了MySQL C API函数。它有一个sample.h和sample.cpp文件,如下所示C++ c++&引用;在使用MySQL C API生成的共享库时,未在此作用域错误中声明,c++,shared-libraries,libmysql,C++,Shared Libraries,Libmysql,我生成了一个共享库,它封装了MySQL C API函数。它有一个sample.h和sample.cpp文件,如下所示 using namespace std; class MysqlInstance { protected: string user; string password; string socket; int port; public: MySqlInstance(string,string,string,port);
using namespace std;
class MysqlInstance
{
protected:
string user;
string password;
string socket;
int port;
public:
MySqlInstance(string,string,string,port);
int connectToDB();
}
在sample.cpp中
MySqlInstance::MySqlInstance(string user,string pass,string sock,int port)
{
this->port=port;
this->user=user;
this->password=pass;
this->socket=sock;
}
MySqlInstance::connectToDB()
{
//code to load libmysqlclient.so from /usr/lib64 and use mysql_init and mysql_real_connect
// functions to connect and "cout" that connection is successful
}
使用:
- g++-fPIC-c sample.cpp
mysql\u config--cflags
- g++-shared-Wl,-soname,libsample.so-o libsample.so sample.o
mysql\u config--libs
#include "sample.h"
using namespace std;
int main()
{
MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}
使用:
- g++-c usesample.cpp-lsample
谢谢好吧,您的类名为
MysqlInstance
,但在main()中您将其称为MysqlInstance
,在您的cpp实现中您有MysqlInstance
C++区分大小写,因此请确保在任何地方都使用正确的标识符。您有一些错误。一是构造函数声明
MySqlInstance(string,string,string,port);
你可能是说
MySqlInstance(string,string,string,int);
然后,在定义中,端口的类型错误:
MySqlInstance::MySqlInstance(string user,string pass,string sock,string port) { .... }
// ^ should be int
然后是类名
class MyqllInstance { .... };
应该是
class MySqlInstance { .... };
然后,您在main
中使用MySQlInstance
,但您的类是MySQlInstance
记住,C++是<强>不区分大小写< /强> ./p>
最后,不要将使用名称空间std
放在头文件中。事实上,不要把它放在任何地方 我不知道发布问题时是否出现复制粘贴错误,但是在你的MysqlInstance
类中有两个port
成员变量。是的..这是一个输入错误..这里的问题是当我在没有MySQL C API的情况下做同样的事情时,我没有得到这些类型的错误。OP也在MysqlInstance
中引用它。所以一个词总共有三种不同的大写形式。包括不同的大小写在标题和源文件的实际图书馆。对不起,人,但我键入它没有复制和粘贴我的实际上是非常大的代码我不能复制粘贴在这里。别介意打字错误。但是这里的问题是作用域,而不是那些愚蠢的int和ot字符串;在创建libsample.com时,它给了我一个非常大的错误列表,所以我是java程序员。对于C++@Sreekar,我建议您修复问题中的剪切和粘贴错误。否则就帮不上忙了。我至少纠正了错误。就是这个VIM编辑器在“MySqlInstance”中犯了一个字母错误,浪费了我3个小时的时间。非常感谢您耐心阅读我的问题,并帮助@juanchopanza