0

我正在使用这里libpq-fe解释的连接到 sql 服务器。 我制作了一个小文件来使用这个 api 检查登录。

这是文件 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}’

我的生成文件:

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 的内容。Thelogin()是我从 中调用的一个简单函数,dispatcher's main它的定义与main我上面提到的相同。

4

1 回答 1

3
void login_check(PGconn, string, string);
void CloseConn(PGconn);

这与您的功能不匹配。采用:

void login_check(PGconn*, string, string);
void CloseConn(PGconn*);

您可以在函数参数中包含指向不完整类型的指针,但不能指向不完整类型。编译器需要知道对象的大小才能正确设置调用/堆栈。它不能使用不完整的类型来做到这一点,但它可以使用指向此类的指针。

于 2012-10-27T09:24:56.493 回答