-2

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

Sreekar
  • 995
  • 1
  • 10
  • 22
  • I don't know if it's a copy-paste error when posting your question, but you have two `port` member variables in your `MysqlInstance` class. – Some programmer dude Nov 22 '12 at 07:15
  • Yeah..thats a typo..the problem here is when I do the samething without MySQL C API and all I didnt get these types of errors. – Sreekar Nov 22 '12 at 07:16

2 Answers2

1

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.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • The OP also referse to it at `MySqlInstance` as well. So in total three different capitalizations of one word. Including different capitalization in the header and source file for the actual library. – Some programmer dude Nov 22 '12 at 07:16
0

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Sorry man but I typed it not copied and pasted mine is actually very big code I cant copy-paste it here. Dont mind the typos. But the problem is scope here not those silly ints ot strings..I removed using namespace std; from header file before It gave me a very big list of errors while creating libsample.so I was java programmer. realatively new to C++ – Sreekar Nov 22 '12 at 07:21
  • @Sreekar I suggest you fix the cut and paste errors in your question. Otherwise it is impossible to help. – juanchopanza Nov 22 '12 at 07:39
  • I corrected the error atlast.It was this VIM editor which made a single letter mistake in "MySqlInstance" and wasted 3 hours of my time. Thank you so much for your patience for reading my question and you help @juanchopanza – Sreekar Nov 22 '12 at 11:43