我是Protocol Buffers(PB)的新手。现在我需要使用PB与2个第三方服务进行通信。 但它失败,此编译错误的工作:协议缓冲区头碰撞
cxs_service.pb.h: ISO C++ forbids declaration of
TSResponse' with no type cxs_service.pb.h: error: invalid use of
::'
我的头文件包括2第三方.h文件看起来像这样:
#include "mob/include/ts_service.pb.h"
#include "pc/include/cxs_service.pb.h"
//### pc/include/cxs_service.pb.h ###
// The compiler seems to find ts_service.pb.h under pc/include successfully
// but it cannot recognize ::pc::TSResponse which is defined in it
# include "ts_service.pb.h"
namespace pc {
class CXSRequest : public ::google::protobuf::Message {
inline const ::pc::TSResponse& ts_response() const;
} // class CXSRequest
} // namespace pc
// i've found that mob/include/ts_service.pb.h, pc/include/ts_service.pb.h have the same header guard.
// Thus pc/include/cxs_service.pb.h really found pc/include/ts_service.pb.h.
// but cannot include it's content because of exactly the same header guard.
#ifndef PROTOBUF_ts_5fservice_2eproto__INCLUDED
#define PROTOBUF_ts_5fservice_2eproto__INCLUDED
#endif
第一第三方PB消息:
// ts_service.proto
package mob;
message TSResponse {
required uint64 id = 1;
}
第二第三方PB信息:
// cxs_service.proto
package pc;
import ts_service.proto;
message CXSRequest {
optional TSResponse ts_response = 1;
}
// which depends on its own ts_service.proto:
// ts_service.proto
package pc;
message TSResponse {
optional string name = 1;
}
似乎编译器无法识别pc :: ts_service.pb.h中定义的:: pc :: TSResponse。编译器首先找到mob的ts_service.pb.h,它定义:: mob :: TSResponse。 – albertgu 2014-11-08 15:20:52