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


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


In conn.h

you must define getConnection

as returning PGconn *

, not PGconn

. PGconn

is an opaque type (your code doesn't need to know anything about this besides the name), so you can't return it or use it by value.



+2


source


Line:

PGconn getConnection();

      

Since it PGConn

is an incomplete type, you cannot define a function that returns it by value, only a pointer to it.

+1


source







All Articles