无效使用不完整的类型"PGconn {aka struct pg_conn}"
invalid use of incomplete type 'PGconn {aka struct pg_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;
}
康恩·
#ifndef CONN_H
#define CONN_H
#include <postgresql/libpq-fe.h>
class Conn
{
public:
const char *connStr;
Conn();
PGconn getConnection();
void closeConn(PGconn *);
};
主.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;
}
错误:无效使用不完整的类型"PGconn {aka struct pg_conn}"
错误:前向声明"PGconn {aka struct pg_conn}"
有什么帮助吗?
在你的conn.h
中,你应该将getConnection
定义为返回一个PGconn *
,而不是PGconn
。 PGconn
是一个不透明的类型(除了名称之外,你的代码不应该知道任何关于它的信息),所以你不能返回它或按值使用它。
在你的 conn.cpp 中,conn::getConnection()
没有返回类型。从你的代码中,我想你需要返回一个指向 PGconn 的指针:
康恩·
class Conn
{
public:
const char *connStr;
Conn();
PGconn* getConnection();
^^ return pointer instead of return by value
void closeConn(PGconn *);
};
康恩.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;
}
行:
PGconn getConnection();
由于PGConn
是不完整的类型,因此不能定义按值返回它的函数,只能定义指向它的指针。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- C++Union/Struct位域的实现和可移植性
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 错误:"Left of getValue must have class/struct/union"
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 数据类型"struct seq<0, 1, 2>{}"含义是什么?
- 将 Vcl::Controls::TCaption aka (System::UnicodeString) 转换为 co
- 为什么 ## aka 令牌粘贴运算符不适用于 C 和 C++ 中的注释?
- 附加使用 Struct 作为"multikey"并将 std::vector 用作映射值的映射
- 从"const void*"到"PVOID {aka void*}"[-fall
- QML/C++/QObject Struct Copy
- 如何在 cpp 中解压缩数字,如果它们是使用 struct.pack(fmt, v1, v2, ..) 打包在 pyth
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- Msgpack能否提供更好的性能和与Python的struct.pack()相同的功能?
- 将向量传递给<struct> Lua 表
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- 无效使用不完整的类型"PGconn {aka struct pg_conn}"