Apache Thrift C++ typedef issue

825 views Asked by At

I'm trying to create a simple thrift server that multiplies two numbers.

I've written the thrift file like this:

namespace cpp tutorial

typedef i32 int

service MultiplicationService
{

    int multiply(1:int n1, 2:int n2),

}

After that I've ran thrift --gen cpp multi.thrift

I renamed the skeleton file to MultiplicationServer.cpp which looks like this:

#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;
}

But when I try to build it I get this error in the multi_types.h file: multiple types in one declaration on the typedef int32_t int line The multi_types.h file looks like this:

/**
 * 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

I tried removing that line, but then I get a lot of undefined reference to errors.

What am I doing wrong?

1

There are 1 answers

0
JensG On BEST ANSWER

You try to typedef the Thrift i32 type to int, which essentially produces a name collision.

So the solution is to no do that:

namespace cpp another_tutorial

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

service MultiplicationService
{
    i32 multiply( 1 : i32 n1, 2 : i32 n2)
}