在“if”内创建 COM 智能指针时出现错误 C2275陈述
为什么我不能
if (IUnknownPtr p = anotherComPtr) {} //error C2275: 'IUnknownPtr' : illegal use of this type as an expression
,而我可以
if (int* a = anotherPointer) {}
IUnknownPtr
是通过 _COM_SMARTPTR_TYPEDEF(IUnknown, __uuidof(IUnknown))
定义的(就像我使用的任何其他智能指针一样)
我如何创建一个 com smartptr 在 if
语句中并验证它是否有效?谢谢。
我用的是 VS
2008这与编码方式是否良好无关,而是与错误 C2275
相关。
Why can't i
if (IUnknownPtr p = anotherComPtr) {} //error C2275: 'IUnknownPtr' : illegal use of this type as an expression
while i can
if (int* a = anotherPointer) {}
IUnknownPtr
is defined throught _COM_SMARTPTR_TYPEDEF(IUnknown, __uuidof(IUnknown))
(like any others smart pointers i use)
How can i create a com smartptr within if
statement and verify is it valid or not? Thank you.
I use VS 2008
p.s. This is not about is it good way of coding or not, it's about error C2275
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法在下面的小程序中重现您在 vs2008 中的编译器错误。您的包含文件、预处理器定义或编译器选项中可能存在不同的内容,从而导致不同的行为。
您可以在 if 语句之外声明一个 IUnknownPtr 类型的简单变量吗?
您可以使用下面的代码创建一个新项目而不出现错误吗?
以下任何一个都可以编译吗?
该错误表明编译器可以看到 IUnknownPtr 的定义,但无法将 IUnknownPtr 的赋值结果解释为 bool。
运算符 = 应该返回 IUnknownPtr& (已分配给的对象)。 _com_ptr_t 定义运算符 bool()。您的 _COM_SMARTPTR_TYPEDEF 是否生成对 _com_ptr_t 或其他类型的引用?您可以通过临时转储预处理器输出(属性/C++/预处理器/预处理到文件)来轻松找到答案
I can't reproduce your compiler error in vs2008 in the small program below. There is likely to be something different in your include files, preprocessor definitions or compiler options which is giving you different behaviour.
Can you declare a simple variable of type IUnknownPtr outside an if statement?
Can you create a new project using the code below without the error?
Does either of the following compile OK?
The error suggests the compiler can see a definition of IUnknownPtr but can't interpret the result of the assignment of an IUnknownPtr as a bool.
operator = should return IUnknownPtr& (the object that's been assigned to). _com_ptr_t defines operator bool(). Are your _COM_SMARTPTR_TYPEDEF generating references to _com_ptr_t or some other type? You can easily find out by temporarily dumping out the preprocessor output (properties/C++/preprocessor/preprocess to a file)
我模糊地记得有关 MSDEV 在 if 语句内声明变量时不遵循 C++ 规范的一些事情(或者它可能是一个 for 循环?)。
它应该在括号末尾被破坏,但事实并非如此。
这可能是一种回溯——您是否尝试过梳理 MSDN 档案?这看起来更像是一种怪癖,而不是有意的行为,除非(正如另一位发帖者评论的那样)你误解了正在发生的事情。
您可以发布实际的代码以及周围的功能吗?
I vaugely remember something about MSDEV not following the C++ specs when it came to declaring a variable inside an if statement (or it might have been a for loop?).
It should have been destructed at the end of the brackets but it was not.
Possibly this is a throwback to that - have you tried combing the MSDN archives? It seems more like a quirk than intentional behaviour, unless (as another poster commented) you have misunderstood what is happening.
Can you post the actual code, complete with surrounding function?
由于这对我来说在 VS 2010 上编译得很好,我假设你可能没有
#include
和/或#include;
。Since this compiles for me just fine on VS 2010, I'm going to assume maybe you didn't
#include <comdef.h>
and/or#include <comdefsp.h>
.