1

I am trying to use google protobuf and they have the following example:

using google::protobuf;

protobuf::RpcChannel* channel;
protobuf::RpcController* controller;
SearchService* service;
SearchRequest request;
SearchResponse response;

void DoSearch() {
  // You provide classes MyRpcChannel and MyRpcController, which implement
  // the abstract interfaces protobuf::RpcChannel and protobuf::RpcController.
  channel = new MyRpcChannel("somehost.example.com:1234");
  controller = new MyRpcController;

  // The protocol compiler generates the SearchService class based on the
  // definition given above.
  service = new SearchService::Stub(channel);

  // Set up the request.
  request.set_query("protocol buffers");

  // Execute the RPC.
  service->Search(controller, request, response, protobuf::NewCallback(&Done));
}

void Done() {
  delete service;
  delete channel;
  delete controller;
}

The error I am getting when I try to implement this code in Visual Studio Express 2008 is:

error C2873: 'google::protobuf' : symbol cannot be used in a using-declaration

Edit: When I do "using namespace google::protobuf;" inside of a function it no longer gives me the error. What I'm confused about is that it doesn't work the way that Google's example (and Stroustrup's in "The C++ Programming Language") seem to indicate.

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79

3 Answers3

4

google::protobuf is probably a namespace. In this case you need to do this.

using namespace google::protobuf;
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

Directly from the documentation:

Visual C++ Concepts: Building a C/C++ Program
Compiler Error C2873
Error Message
'symbol' : symbol cannot be used in a using-declaration
A using directive is missing a namespace keyword. This causes the compiler to misinterpret the code as a using declaration rather than a using directive.

More info on the difference.

Andrew Khosravian
  • 1,069
  • 6
  • 13
0

(1) According to Microsoft, the C2873 means;

'symbol' : symbol cannot be used in a using-declaration A using directive is missing a namespace keyword. This causes the compiler to misinterpret the code as a using declaration rather than a using directive.

(2) Also when I had C2873 with C2039 (I tried to merge CEF3 and Cinder in Visual Studio 2010), somehow I bypassed the both error by changing Properties->Configuration Properties->C/C++->Code Generation;

Enable Minimal Rebuild: Yes(/Gm), Enable C++ Exception: Yes(/EHsc), Enable Function-Level Linking: empty

Cloud Cho
  • 1,594
  • 19
  • 22