Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ protobuf中的错误处理_C++_Protocol Buffers - Fatal编程技术网

C++ protobuf中的错误处理

C++ protobuf中的错误处理,c++,protocol-buffers,C++,Protocol Buffers,我不熟悉protobuf(C++),尝试用protobuf编写第一个测试程序。 代码是 #包括 #包括 #包括“proto/req.pb.h” 使用名称空间std; 使用名称空间google::protobuf; int main(int argc,字符**argv){ std::string s=“asdfasdf”; 自动MyLogHandler=[](google::protobuf::LogLevel,常量字符*文件名,int行,常量std::字符串和消息) { std::coutPro

我不熟悉protobuf(C++),尝试用protobuf编写第一个测试程序。 代码是

#包括
#包括
#包括“proto/req.pb.h”
使用名称空间std;
使用名称空间google::protobuf;
int main(int argc,字符**argv){
std::string s=“asdfasdf”;
自动MyLogHandler=[](google::protobuf::LogLevel,常量字符*文件名,int行,常量std::字符串和消息)
{

std::coutProtobuf解析失败有两个原因:

  • 输入数据缺少必填字段。在这种情况下,Protobuf库写入一条描述问题的日志消息,然后返回false。在这种情况下,日志处理程序将收到错误消息

  • 输入数据不是有效的Protobuf(它已损坏,或者根本不是Protobuf)。在这种情况下,Protobuf库只返回false而不返回任何错误消息。该库在此处无法提供任何有用的信息。如果发生这种情况,最好的调试方法是在序列化消息之后和解析消息之前转储确切的字节,然后查找差异


这不是protobuf消息。protobobuf库在没有任何符号的情况下无声返回false是一种奇怪的行为。@Nikolay-是的,您的伪输入数据属于第二类。正如我所说,protobuf库没有给您任何信息,因为没有有用的信息可以提供。
false
的意思是“数据不是protobuf”。仅此而已。(这不是“静默的”。“静默的”将是返回
true
,即使消息无效。)
#include <cstdlib>
#include <iostream>
#include "proto/req.pb.h"

using namespace std;
using namespace google::protobuf;

int main(int argc, char** argv) {
      std::string s = "asdfasdf";

        auto MyLogHandler = [] (google::protobuf::LogLevel level, const char* filename, int line, const std::string& message)
        {
            std::cout << "message " << message << std::endl;
        };
        google::protobuf::SetLogHandler(MyLogHandler);

        Request req;
        if ( req.ParseFromString(s)){
            cout << "Parse - OK" << endl;
        }else{
               cout << "Parse - ERROR" << endl;
        }
        return 0;
    }