[英]invalid use of incomplete type 'PGconn {aka struct pg_conn}'
I have two classes, Main class and a connection class as: 我有两个类,Main类和连接类:
Conn.cpp: 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 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 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: invalid use of incomplete type 'PGconn {aka struct pg_conn}' 错误:无效使用不完整类型'PGconn {aka struct pg_conn}'
error: forward declaration of 'PGconn {aka struct pg_conn}' 错误:'PGconn {aka struct pg_conn}'的前向声明
Any help? 有帮助吗?
In your conn.h
, you should define getConnection
as returning a PGconn *
, not PGconn
. 在你的
conn.h
,你应该将getConnection
定义为返回PGconn *
,而不是PGconn
。 PGconn
is an opaque type (your code should not know anything about it besides the name), so you can't return it or use it by value. PGconn
是一个opaque类型(除了名称之外,你的代码不应该知道它的任何内容),所以你不能返回它或按值使用它。
The line: 这条线:
PGconn getConnection();
Since PGConn
is an incomplete type, you cannot define a function that returns it by value, only a pointer to it. 由于
PGConn
是一个不完整的类型,因此您无法定义一个按值返回的函数,只能指向它的指针。
In your conn.cpp, conn::getConnection()
has no return type. 在你的conn.cpp中,
conn::getConnection()
没有返回类型。 From your code, I guess you need to return a pointer to PGconn: 从你的代码中,我猜你需要返回一个指向PGconn的指针:
conn.h conn.h
class Conn
{
public:
const char *connStr;
Conn();
PGconn* getConnection();
^^ return pointer instead of return by value
void closeConn(PGconn *);
};
conn.cpp 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.