List Apache Thrift:返回列表/容器

List Apache Thrift:返回列表/容器,list,thrift,List,Thrift,我制作了一个简单的储蓄文件,如下所示: 节俭测试。节俭 namespace cpp thrifttest namespace d thrifttest namespace java thrifttest namespace php thrifttest namespace perl thrifttest service Test { list<i64> ping(); } void ping(std::vector<int64_t> & _retu

我制作了一个简单的储蓄文件,如下所示:

节俭测试。节俭

namespace cpp thrifttest
namespace d thrifttest
namespace java thrifttest
namespace php thrifttest
namespace perl thrifttest

service Test {

    list<i64> ping();

}
void ping(std::vector<int64_t> & _return) {
    // Your implementation goes here
    printf("ping\n");
}
名称空间cpp节俭测试
名称空间节俭测试
命名空间java节俭测试
名称空间php节俭测试
名称空间perl节俭测试
服务测试{
列表ping();
}
在壳牌公司,有“节俭--gen cpp节俭测试,节俭”

但是,当我查看gen cpp/Test_server.skeleton.cpp时,它使i64列表成为一个参数,而不是返回类型:

测试服务器.skeleton.cpp(节选)

void ping(标准::向量和返回){
//你的实现就在这里
printf(“ping\n”);
}
在我的server.cpp程序中,在生成一个返回“std::vector&”的函数ping()后,编译器会抱怨

错误:无法分配抽象类型为“TestHandler”的对象 server.cpp:30:7:注意:因为以下虚拟函数在“TestHandler”中是纯函数:

这是server.cpp的完整代码 server.cpp

#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TTransportUtils.h>

#include <iostream>
#include <stdexcept>
#include <sstream>

#include "gen-cpp/Test.h"

using namespace std;
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 thrifttest;

using namespace boost;

unsigned long giant[100];

class TestHandler : virtual public TestIf {
 public:
  TestHandler() {
      for (int i = 0; i < 100; i++) {
          giant[i] = -1;
      }
  }


  std::vector<int64_t> & ping() {
        return (std::vector<int64_t> &)giant;
    }

  void ping(std::vector<int64_t> & bla) {}
};

int main(int argc, char **argv) {

  shared_ptr<TestHandler> handler(new TestHandler());
  shared_ptr<TProcessor> processor(new TestProcessor(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);

  printf("Starting the server...\n");
  server.serve();
  printf("done.\n");
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“gen cpp/Test.h”
使用名称空间std;
使用名称空间apache::thrift;
使用名称空间apache::thrift::protocol;
使用名称空间apache::thrift::transport;
使用名称空间apache::thrift::server;
使用boost::shared\u ptr;
使用名称空间节俭测试;
使用名称空间boost;
无符号长巨人[100];
类TestHandler:虚拟公共TestIf{
公众:
TestHandler(){
对于(int i=0;i<100;i++){
巨[i]=-1;
}
}
std::vector&ping(){
返回(std::vector&)巨人;
}
无效ping(std::vector&bla){}
};
int main(int argc,字符**argv){
共享的ptr处理程序(新的TestHandler());
共享的ptr处理器(新的TestProcessor(handler));
共享的ptr服务器传输(新的TServerSocket(端口));
共享的ptr传输工厂(新的TBufferedTransportFactory());
共享的ptr协议工厂(新的TBinaryProtocolFactory());
TSimpleServer服务器(处理器,
服务器传输,
运输厂,
原型工厂);
printf(“正在启动服务器…\n”);
server.service();
printf(“完成。\n”);
返回0;
}

我无意中看到一篇文章()讨论了这个确切的问题(尽管答案在我的案例中不起作用)。事实证明,thrift使用了一种按引用传递的语法,尽管如此。下面是一个例子:

.thrift文件

service Test {

    list<string> ping();
}
void ping(std::vector<string> & _return) {
    _return.push_back("hello");    //initialize the vector _return with values "hello","world"
    _return.push_back("world");
}
std::vector<string> result;     //create vector "result" for storing the values
client.ping(result);
printf("%s %s!\n", result[0].c_str(), result[1].c_str());   //c_str() turns the vector string into a C-style string
服务测试{
列表ping();
}
服务器端

service Test {

    list<string> ping();
}
void ping(std::vector<string> & _return) {
    _return.push_back("hello");    //initialize the vector _return with values "hello","world"
    _return.push_back("world");
}
std::vector<string> result;     //create vector "result" for storing the values
client.ping(result);
printf("%s %s!\n", result[0].c_str(), result[1].c_str());   //c_str() turns the vector string into a C-style string
void ping(标准::向量和返回){
_return.push_back(“hello”);//使用值“hello”、“world”初始化向量_return
_返回。推回(“世界”);
}
客户端

service Test {

    list<string> ping();
}
void ping(std::vector<string> & _return) {
    _return.push_back("hello");    //initialize the vector _return with values "hello","world"
    _return.push_back("world");
}
std::vector<string> result;     //create vector "result" for storing the values
client.ping(result);
printf("%s %s!\n", result[0].c_str(), result[1].c_str());   //c_str() turns the vector string into a C-style string
std::向量结果//创建用于存储值的向量“结果”
client.ping(结果);
printf(“%s%s!\n”,结果[0].c_str(),结果[1].c_str())//c_str()将向量字符串转换为c样式字符串
客户端输出(启动服务器后)

你好,世界


谢谢你节俭,因为没有文件,我玩得很开心#4小时哭哭啼啼

除了您的问题之外,直接返回容器类还有一些缺点


相反,我会将容器包装到一个
结构中
,然后返回该结构。通过这种方式,您可以在以后添加任何字段,而不需要新的服务方法。此外,如果有必要,可以省略容器本身-出于技术原因,您不能使用Thrift返回
NULL
容器。

+1对于
,感谢Thrift缺少文档,我非常高兴#4小时呜咽和哭泣
。目前处于同一阶段。:'(如果你需要什么,我可以试着帮你:)首先,开源也来自你。这是双向的。哭不会让事情变得更好,也不会帮助有同样问题的人。接下来,还有可用的文档(是的,还远远不够完美),如教程和其他示例代码,如书籍、白皮书和博客文章,所有这些都与其他SO问题多次链接。最后一点,你可以随时在Thrift邮件列表和#Thrift Freenode频道上询问。也许这还不够,你更清楚吗?太好了,非常欢迎您,我们接受补丁和贡献!感谢所有的资源!我已经有一段时间没用过节俭了,但我相信这些会对其他人有用的