【发布时间】:2015-05-24 17:07:18
【问题描述】:
为什么下面的代码会报如下错误?
为什么类型需要完整才能转换为void?
struct Incomplete;
class Class
{
virtual void foo(Incomplete &incomplete)
{
(void) incomplete;
throw std::logic_error("not implemented");
}
};
错误:
error C2027: use of undefined type 'Incomplete'
see declaration of 'Incomplete'
【问题讨论】:
-
为什么会是更有趣的问题。只需改用
(void)&incomplete。 -
FWIW、GCC 和 clang 没有任何问题。
-
在 VS2013 中不编译,但在 Microsoft's online compiler 中编译。所以这可能只是一个错误。
-
有趣。你甚至不需要投到
void。试试void foo(Incomplete &incomplete) { incomplete; },它会给你同样的错误。 -
进一步表明这似乎是编译器的一个错误区域:尝试声明一个
Incomplete数组,例如Incomplete x[1];上面写着error C2148: total size of array must not exceed 0x7fffffff bytes,对于 GCC 所称的elements of array 'Incomplete x [1]' have incomplete type,这是一条非常奇怪的消息。
标签: c++ void incomplete-type