'Forward Declaration of Struct' 在检索 libevent 连接信息时

'Forward Declaration of Struct' while retrieving libevent connection Information

我正在尝试检索在 libevent:

注册的回调中请求的原始连接信息
#include <evhttp.h>
#include <iostream>

//Process a request
void process_request(struct evhttp_request *req, void *arg){

    //Get the request type (functions correctly)
    std::cout << req->type << std::endl;

    //Get the address and port requested that triggered the callback
    //When this is uncommented, the code no longer compiles and throws
    //the warning below
    struct evhttp_connection *con = req->evcon;
    std::cout << con->address << con->port << std::endl;
}

int main () {

    //Set up the server
    struct event_base *base = NULL;
    struct evhttp *httpd = NULL;
    base = event_init();
    if (base == NULL) return -1;
    httpd = evhttp_new(base);
    if (httpd == NULL) return -1;

    //Bind the callback
    if (evhttp_bind_socket(httpd, "0.0.0.0", 12345) != 0) return -1;
    evhttp_set_gencb(httpd, process_request, NULL);

    //Start listening
    event_base_dispatch(base);
    return 0;
}

但是,我收到以下错误:

$g++ -o basic_requests_server basic_requests_server.cpp -lpthread -levent -std=c++11

basic_requests_server.cpp:45:18: error: invalid use of incomplete type ‘struct evhttp_connection’
  std::cout << con->address << con->port << std::endl;
              ^
In file included from /usr/include/evhttp.h:41:0,
             from basic_requests_server.cpp:1:
/usr/include/event2/http.h:427:8: error: forward declaration of ‘struct evhttp_connection’
 struct evhttp_connection *evhttp_connection_base_new(

为什么我不能访问这个结构的元素?

Why can't I access the elements of this struct?

据我了解,连接(即 struct evhttp_connection)仅供内部使用。
您不应直接使用它们或它们的字段,但您可以获得指向连接的指针并传递该指针。
这是故意的,以避免客户端绑定到连接的内部表示(因此可以通过这种方式静默更改)。
这就是为什么类型实际上没有暴露的原因。您可以将其视为一个 不透明指针 不允许取消引用。

有关详细说明,请参阅 here