c++/postgresql problem initializing private member
Hi there,
I need some help. I'm trying to write a simple class in c++ using the library libpqxx for c++ that is going to have a connection object as a private member and 2 methods, the first is going to make the connection (calls the connection's constructor) and the second is going to send queries. The problem is with the first one.
This is my piece of code:
Code:
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
class test {
private:
connection conn;
public:
void foo(){
conn=connection("dbname=temp");
}
result boo() {
work trans(conn, "Demo");
result res=trans.exec("select * from profile;");
trans.commit();
return res;
}
};
int main() {
test t;
t.foo();
result r;
r=t.boo();
return 0;
}
I tried to compile the above with g++ and it gives the following output:
Code:
/usr/local/include/pqxx/connection_base.hxx: In copy constructor ‘pqxx::basic_connection<pqxx::connect_direct>::basic_connection(const pqxx::basic_connection<pqxx::connect_direct>&)’:
/usr/local/include/pqxx/connection_base.hxx:904: error: ‘pqxx::connection_base::connection_base(const pqxx::connection_base&)’ is private
/usr/local/include/pqxx/basic_connection.hxx:50: error: within this context
test.cpp: In member function ‘void test::foo()’:
test.cpp:12: note: synthesized method ‘pqxx::basic_connection<pqxx::connect_direct>::basic_connection(const pqxx::basic_connection<pqxx::connect_direct>&)’ first required here
/usr/local/include/pqxx/connection_base.hxx: In member function ‘pqxx::basic_connection<pqxx::connect_direct>& pqxx::basic_connection<pqxx::connect_direct>::operator=(const pqxx::basic_connection<pqxx::connect_direct>&)’:
/usr/local/include/pqxx/connection_base.hxx:905: error: ‘pqxx::connection_base& pqxx::connection_base::operator=(const pqxx::connection_base&)’ is private
/usr/local/include/pqxx/basic_connection.hxx:50: error: within this context
test.cpp: In member function ‘void test::foo()’:
test.cpp:12: note: synthesized method ‘pqxx::basic_connection<pqxx::connect_direct>& pqxx::basic_connection<pqxx::connect_direct>::operator=(const pqxx::basic_connection<pqxx::connect_direct>&)’ first required here
I tried also to change the private member and have a pointer to connection like this:
Code:
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
class test {
private:
connection* conn;
public:
void foo(){
conn=new connection("dbname=temp");
}
result boo() {
work trans(conn, "Demo");
result res=trans.exec("select * from profile;");
trans.commit();
return res;
}
};
int main() {
test t;
t.foo();
result r;
r=t.boo();
return 0;
}
and the connection was ok but the function that sends queries (the boo() one) had the problem. Output of g++:
Code:
tre.cpp: In member function ‘pqxx::result test::boo()’:
tre.cpp:17: error: no matching function for call to ‘pqxx::transaction<read_committed>::transaction(pqxx::connection*&, const char [5])’
/usr/local/include/pqxx/transaction.hxx:98: note: candidates are: pqxx::transaction<ISOLATIONLEVEL>::transaction(pqxx::connection_base&) [with pqxx::isolation_level ISOLATIONLEVEL = read_committed]
/usr/local/include/pqxx/transaction.hxx:93: note: pqxx::transaction<ISOLATIONLEVEL>::transaction(pqxx::connection_base&, const std::string&) [with pqxx::isolation_level ISOLATIONLEVEL = read_committed]
/usr/local/include/pqxx/transaction.hxx:83: note: pqxx::transaction<read_committed>::transaction(const pqxx::transaction<read_committed>&)
Finally, I have to mention that when I use the c library (libpq-fe.h) I can write this kind of program (with this API) but for many reasons I want to use the c++ library for postgres (e.g. the first one doesn't support exceptions).
Sorry for my big post. I would appreciate any help.
Thanks in advance.