C++ “使用” Visual Studio 2008 中的声明
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
google::protobuf
可能是一个命名空间
。 在这种情况下,您需要这样做。google::protobuf
is probably anamespace
. In this case you need to do this.直接来自文档:
有关差异的更多信息。
Directly from the documentation:
More info on the difference.
(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