c++ " was not declared in this scope error while using shared library generated by MySQL C API [closed]

StackOverflow https://stackoverflow.com/questions/13507466

  •  01-12-2021
  •  | 
  •  

Question

I generated a shared library which wraps MySQL C API functions. It has a sample.h and sample.cpp files like this

using namespace std;
class MysqlInstance
{
    protected:
    string user;
    string password;
    string socket;
    int port;

    public:
    MySqlInstance(string,string,string,port);
    int connectToDB();
 }

In 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
}

Used:

  • g++ -fPIC -c sample.cpp mysql_config --cflags

  • g++ -shared -Wl,-soname,libsample.so -o libsample.so sample.o mysql_config --libs

Now libsample.so is generated and I moved it to /usr/lib Now I created a small cpp file which uses this shared library in the same directory. usesample.cpp

#include "sample.h"
using namespace std;
int main()
{
 MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}

Used:

  • g++ -c usesample.cpp -lsample

It is giving me this error:

error: âMysqlInstanceâ was not declared in this scope error: object was not declared in this scope

Thanks

Was it helpful?

Solution 2

You have a few errors. One, is the constructor declaration

 MySqlInstance(string,string,string,port);

You probably mean

MySqlInstance(string,string,string,int);

Then, the definition, you have the type of port wrong:

MySqlInstance::MySqlInstance(string user,string pass,string sock,string port) { .... }
                                                              //   ^ should be int

Then, the class name

class MyqllInstance { .... };

should be

class MySqlInstance { .... };

Then, you are using MySQlInstance in main but your class is MySqlInstance.

Remember, C++ is not case insensitive.

Finally, do not put using namespace std in a header file. In fact, do not put it anywhere.

OTHER TIPS

Well, your class is named MysqlInstance but in your main() you refer to it as MySQlInstance, and in your cpp implementation you have MySqlInstance.

C++ is case-sensitive, so make sure you use the correct identifier everywhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top