Simple Newbie question about accessing a Variable out of a class of a class

C

Christian Maier

Hi

After compiling "Hello World", I am on the way to code a shared
library - but now I have some trouble. I have a class which is holding
a Database connection. In this class there are other classes holding
data from sql statements. For now I cannot access the connection
object in the classes of the "connectonholding"-class, which is what I
need.

Here I paste my code and marked the sentence where the problem is,
probably you can better imagine what I want.

Thanks for your Help!
Christian Maier


//foo.h
#include <pqxx/pqxx>

using namespace PGSTD;
using namespace pqxx;

typedef long long llong;
class FiMi
{
public:
void setDB(string);
void setHost(string);
void setPort(string);
void setUser(string);
void setPasswd(string);
void setSchema(string);
void doConnect();
void doDisconnect();
connection * getConnection;
private:
string db;
string host;
string port;
string user;
string passwd;
string schema;
string connectionstring;
connection dbCon(string);

public:
class fProvider
// I must have access to the connection object
{
public:
long long getProvider(string); //from
caption
void getAllProviders(string[]);
void getAllProviderTypes(string[]);
long long getProvideType(string); //from
caption
void addProvider(string,long long, string); //caption,
ptype_id, additional
void delProvider(long long); //via
provider_id
void updProvider(long long); //via
provider_id
};
};


//foo.cpp
#include <pqxx/pqxx>
#include "foo.h"
using namespace PGSTD;
using namespace pqxx;

typedef long long llong;
void FiMi::doConnect()
{

connectionstring = "dbname=" + db + " " +
"host=" + host + " " +
"port=" + port + " " +
"user=" + user + " " +
"password=" + passwd;

try
{
cout << "ich bin hier " + connectionstring + " \n";
connection dbCon(connectionstring);
cout << "connected to " << dbCon.dbname() << " \n";

//probably I want to use the connection from client code for
native sqls ....
getConnection = &dbCon;
}
catch (const broken_connection &e)
{
cout << "beim verbinen ist folgender fehler passiert:\n";
throw logic_error( e.what() );
}

};


llong FiMi::fProvider::getProvider(string caption)
{
try
{
/* ####################### HERE IS WHAT I NEED
###########################
THE dbCon Object is no access habe her which is what i
need, how to do this
cout << dbCon.Database << endl;

######################################################################
*/
return 0;
}
catch (const sql_error &e)
{
cerr << "SQL error: " << e.what() << endl
<< "Query was: '" << e.query() << "'" << endl;
return -1;
}
};

int main( int argc, char *argv[] )
{
FiMi a;
return 0;
}
 
J

John Harrison

Christian said:
Hi

After compiling "Hello World", I am on the way to code a shared
library - but now I have some trouble.

That's a big leap.

I have a class which is holding
a Database connection. In this class there are other classes holding
data from sql statements. For now I cannot access the connection
object in the classes of the "connectonholding"-class, which is what I
need.

Well it no different from any case where you have information in one
place which you need to access in another place. You must pass the
connection object as a parameter to the other class.
llong FiMi::fProvider::getProvider(string caption)
{
try
{
/* ####################### HERE IS WHAT I NEED
###########################
THE dbCon Object is no access habe her which is what i
need, how to do this
cout << dbCon.Database << endl;

######################################################################
*/

Two changes, move the fProvider class out of the FiMi. As you've already
found having one class inside another does not give the inside access
any special access to members of the outer class C++ is not Java).
Secondly add a FiMi parameter to your getProvider method, like this.

llong fProvider::getProvider(string caption, FiMi& dbCon)

This is going to be a long journey, and you're only just starting.

john
 
C

Christian Maier

Thanks for the hint, I have done a Step back and switched fom c++ to
c. This seems to be easier for me.

Chris
 
J

John Harrison

Christian said:
Thanks for the hint, I have done a Step back and switched fom c++ to
c. This seems to be easier for me.

Chris

No problem. I learned C before C++. I found it very helpful in switching
to C++ to think 'how would I write the equivlent code in C'. In fact
early C++ compilers did exactly that, they translated C++ into C. Now I
just think in C++ though and I'm much happier but for some reason some
people still perfer C.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top