C++ “使用” Visual Studio 2008 中的声明

发布于 2024-07-14 02:34:39 字数 1180 浏览 14 评论 0原文

我正在尝试使用 google protobuf,他们有以下示例:

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

当我尝试在 Visual Studio Express 2008 中实现此代码时遇到的错误是:

错误 C2873:“google::protobuf”: 符号不能用在 使用声明

编辑:当我执行“using namespace google::protobuf;”时 在函数内部它不再给我错误。 我感到困惑的是,它并不像 Google 的示例(以及 Stroustrup 在“C++ 编程语言”中的示例)所示的那样工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-07-21 02:34:39

google::protobuf 可能是一个命名空间。 在这种情况下,您需要这样做。

using namespace google::protobuf;

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

using namespace google::protobuf;
听,心雨的声音 2024-07-21 02:34:39

直接来自文档

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.

有关差异的更多信息。

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.

森罗 2024-07-21 02:34:39

(1) 根据 Microsoft 的说法,C2873 是指;

'symbol' : 符号不能在 using 声明中使用 using 指令缺少命名空间关键字。 这会导致编译器将代码误解为 using 声明而不是 using 指令。

(2) 另外,当我使用 C2873 和 C2039 时(我尝试在 Visual Studio 2010 中合并 CEF3 和 Cinder),不知何故,我通过更改“属性”->“配置属性”->“C/C++”->“代码生成”绕过了这两个错误;

启用最小重建:是(/Gm),启用 C++ 异常:是(/EHsc),启用功能级链接:空

(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文