0

我正在尝试创建一个将两个数字相乘的简单节俭服务器。

我已经像这样编写了节俭文件:

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错误。

我究竟做错了什么?

4

1 回答 1

0

您尝试将 Thrifti32类型定义为int,这实际上会产生名称冲突。

所以解决方案是不这样做:

namespace cpp another_tutorial

// typedef i32 int - don't try this at home

service MultiplicationService
{
    i32 multiply( 1 : i32 n1, 2 : i32 n2)
}
于 2014-12-01T00:12:48.513 回答