Invalid use of incomplete type 'PGconn {aka struct pg_conn}'
I have two classes: Main class and connection class:
conn.cpp:
#include "conn.h"
#include <postgresql/libpq-fe.h>
Conn::getConnection()
{
connStr = "dbname=test user=postgres password=Home hostaddr=127.0.0.1 port=5432";
PGconn* conn;
conn = PQconnectdb(connStr);
if(PQstatus(conn) != CONNECTION_OK)
{
cout << "Connection Failed.";
PQfinish(conn);
}
else
{
cout << "Connection Successful.";
}
return conn;
}
conn.h
#ifndef CONN_H
#define CONN_H
#include <postgresql/libpq-fe.h>
class Conn
{
public:
const char *connStr;
Conn();
PGconn getConnection();
void closeConn(PGconn *);
};
main.cpp
#include <iostream>
#include <postgresql/libpq-fe.h>
#include "conn.h"
using namespace std;
int main()
{
PGconn *connection = NULL;
Conn *connObj;
connection = connObj->getConnection();
return 0;
}
error: illegal use of incomplete type 'PGconn {aka struct pg_conn}'
error: pre-declaration 'PGconn {aka struct pg_conn}'
Any help?
+3
user2021471
source
to share
3 answers
In your conn.cpp conn::getConnection()
has no return type. From your code, I think you need to return a pointer to PGconn:
conn.h
class Conn
{
public:
const char *connStr;
Conn();
PGconn* getConnection();
^^ return pointer instead of return by value
void closeConn(PGconn *);
};
conn.cpp
PGconn* Conn::getConnection()
^^^^^^ // return PGconn pointer
{
connStr = "dbname=test user=postgres password=Home hostaddr=127.0.0.1 port=5432";
PGconn* conn = NULL;
conn = PQconnectdb(connStr);
if(PQstatus(conn) != CONNECTION_OK)
{
cout << "Connection Failed.";
PQfinish(conn);
}
else
{
cout << "Connection Successful.";
}
return conn;
}
+1
source to share