我正在使用libpq-fe
连接到sql server,如here所述
我用一个小文件来检查登录。
这是文件psql.cpp:
#include <iostream>
#include <libpq-fe.h>
#include <string>
using namespace std;
void CloseConn(PGconn *conn)
{
PQfinish(conn);
getchar();
}
PGconn *ConnectDB(string user="postgres",string password="123321",string dbname="bridge",string hostaddr="127.0.0.1",string port="5432")
{
PGconn *conn = NULL;
string s = "user=" + user + " password=" + password + " dbname=" +dbname + " hostaddr=" + hostaddr + " port=" + port;
// Make a connection to the database
conn = PQconnectdb(s.c_str());
// Check to see that the backend connection was successfully made
if (PQstatus(conn) != CONNECTION_OK)
{
cout << "Connection to database failed.\n";
CloseConn(conn);
}
cout << "Connection to database - OK\n";
return conn;
}
void login_check(PGconn *conn, string username, string password)
{
string query = "SELECT * FROM login where plid='" + username + "'";
PGresult *res = PQexec(conn,query.c_str());
if(PQresultStatus(res) == PGRES_TUPLES_OK)//successful completion of a command returning data
{
cout << "query executed successfully\n";
int row = PQntuples(res); // number of rows in the output of the query
cout<<row<<endl;
if (row != 1)
{
//wrong username
}
else
{
cout<<PQgetvalue(res,0,1)<<endl;
if( !(string(PQgetvalue(res,0,1)).compare(password)) )//return 0 on equality
{
cout<<"valid user";
}
}
}
// Clear result
PQclear(res);
}
我把hits main函数放在这个文件中,一切正常:
int main()
{
PGconn *conn = NULL;
//conn = ConnectDB("postgres","123321","bridge","127.0.0.1","5432");
conn = ConnectDB("postgres","123321","bridge","127.0.0.1","5432");
if (conn != NULL) {
login(conn, "11111000", "abcd");
CloseConn(conn);
}
return 0;
}
现在我想将此文件包含在另一个.cpp
文件中,因此我创建了一个psql.h
文件:
#include <libpq-fe.h>
#include <string>
using namespace std;
PGconn *ConnectDB(string ,string, string, string, string);
void login_check(PGconn, string, string);
void CloseConn(PGconn);
对于使用psql.h
文件,我在psql.cpp
标题中所做的更改是:
#include "psql.h"
#include <iostream>
void CloseConn(PGconn *conn)
{
...
...
我从中删除了main
函数。
现在在我的新文件中 - dispatcher.cpp
我想要包含此文件,因此更改其标题:
#include "psql.h"
我将上面提到的main
函数的内容放在此文件的main
函数中。当我编译这个文件时,我得到了错误:
dispatcher.cpp: In function ‘void login(int)’:
dispatcher.cpp:154:45: error: parameter 1 of ‘void login_check(PGconn, std::string, std::string)’ has incomplete type ‘PGconn {aka pg_conn}’
dispatcher.cpp:155:23: error: parameter 1 of ‘void CloseConn(PGconn)’ has incomplete type ‘PGconn {aka pg_conn}’
我的makefile:
dispatcher:dispatcher.o access.o psql.o
g++ dispatcher.o access.o psql.o -pthread -I /usr/include/postgresql -lpq -o ./bin/dispatcher
dispatcher.o:dispatcher.cpp
g++ -I /usr/include/postgresql -lpq -c dispatcher.cpp
access.o:access.cpp access.h
g++ -c access.cpp
psql.o:psql.cpp psql.h
g++ -c psql.cpp -I /usr/include/postgresql -lpq
您可以忽略access.o
它只包含配件。我知道我c
使用c++
进行编程。 为什么我收到此错误。是CPP特有的。 login()
是一个简单的函数,我从dispatcher's main
调用它的定义与我上面提到的main
相同。
答案 0 :(得分:3)
void login_check(PGconn, string, string);
void CloseConn(PGconn);
这与您的功能不符。使用:
void login_check(PGconn*, string, string);
void CloseConn(PGconn*);
您可以在函数参数中指向不完整类型,但不是不完整类型。编译器需要知道对象的大小才能正确设置调用/堆栈。它不能用不完整的类型做到这一点,但它可以指向这样的。