Apache Thrift C++ typedef 问题
Posted
技术标签:
【中文标题】Apache Thrift C++ typedef 问题【英文标题】:Apache Thrift C++ typedef issue 【发布时间】:2014-11-30 18:57:57 【问题描述】:我正在尝试创建一个将两个数字相乘的简单 thrift 服务器。
我已经这样写了 thrift 文件:
namespace cpp tutorial
typedef i32 int
service MultiplicationService
int multiply(1:int n1, 2:int n2),
在那之后我跑了thrift --gen cpp multi.thrift
我将骨架文件重命名为MultiplicationServer.cpp
,如下所示:
#include "MultiplicationService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
using namespace ::tutorial;
class MultiplicationServiceHandler : virtual public MultiplicationServiceIf
public:
MultiplicationServiceHandler()
// Your initialization goes here
int multiply(const int n1, const int n2)
return n1 * n2;
;
int main(int argc, char **argv)
int port = 9090;
shared_ptr<MultiplicationServiceHandler> handler(new MultiplicationServiceHandler());
shared_ptr<TProcessor> processor(new MultiplicationServiceProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
但是当我尝试构建它时,我在 multi_types.h 文件中收到此错误:multiple types in one declaration
在 typedef int32_t int 行上
multi_types.h 文件如下所示:
/**
* Autogenerated by Thrift Compiler (0.9.2)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
#ifndef multi_TYPES_H
#define multi_TYPES_H
#include <iosfwd>
#include <thrift/Thrift.h>
#include <thrift/TApplicationException.h>
#include <thrift/protocol/TProtocol.h>
#include <thrift/transport/TTransport.h>
#include <thrift/cxxfunctional.h>
namespace tutorial
typedef int32_t int;
// namespace
#endif
我尝试删除该行,但随后出现很多 undefined reference to
错误。
我做错了什么?
【问题讨论】:
哪一行有问题?类型定义? 是 typedef 行。 【参考方案1】:您尝试将 Thrift i32
类型定义为 int
,这实际上会产生名称冲突。
所以解决办法是不这样做:
namespace cpp another_tutorial
// typedef i32 int - don't try this at home
service MultiplicationService
i32 multiply( 1 : i32 n1, 2 : i32 n2)
【讨论】:
以上是关于Apache Thrift C++ typedef 问题的主要内容,如果未能解决你的问题,请参考以下文章
用 C++ 编写的 Apache Thrift 服务器抛出 TTransportException