C++ ';Struct'的转发声明;在检索libevent连接信息时

C++ ';Struct'的转发声明;在检索libevent连接信息时,c++,c++11,struct,callback,libevent,C++,C++11,Struct,Callback,Libevent,我正在尝试检索在使用libevent注册的回调中请求的原始连接信息: #包括 #包括 //处理请求 无效进程请求(结构evhttp请求*req,无效*arg){ //获取请求类型(功能正确) 标准::cout型evcon; std::cout地址端口 为什么我不能访问此结构的元素 据我所知,连接(即struct evhttp_connection)仅用于内部目的。 您不应该直接使用它们或它们的字段,但您可以获取指向连接的指针并传递该指针。 这样做是有目的的,以避免客户端绑定到连接的内部表示(这样

我正在尝试检索在使用libevent注册的回调中请求的原始连接信息:

#包括
#包括
//处理请求
无效进程请求(结构evhttp请求*req,无效*arg){
//获取请求类型(功能正确)
标准::cout型evcon;
std::cout地址端口
为什么我不能访问此结构的元素

据我所知,连接(即
struct evhttp_connection
)仅用于内部目的。
您不应该直接使用它们或它们的字段,但您可以获取指向连接的指针并传递该指针。
这样做是有目的的,以避免客户端绑定到连接的内部表示(这样可以以静默方式更改)。
这就是为什么该类型实际上没有公开。您可以将其视为不允许取消引用的不透明指针


请参阅以获得深入的解释。

@skypjack:让它成为一个answer@MartinBonner很公平,完成了。
#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(