不完整的类型与Clang编译c ++ 11代码 [英] Incomplete types with Clang compiling c++11 code

查看:173
本文介绍了不完整的类型与Clang编译c ++ 11代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如本网站所示,Clang将不支持使用C + +11:

  class Node {
vertex< Node>儿童;
};

会发生错误:

 字段有不完整的类型'Node'

C ++ 98和其他编译器,如C ++ 11中的gcc。



我知道我可以使用

  vertex< Node *> 

,但目前我与C ++ 98中的旧代码有一些不兼容问题。我的问题是,(1)我可以使用Clang在C ++ 11中编译这样的代码吗? (2)我认为树结构不可避免地需要如上定义,没有这种特性的支持,我如何实现这样的树结构?






更新:



对于忘记给定顶点定义很抱歉,以下代码如何:

  class Node {
vector< Node>儿童;
};

只需将顶点更改为容器向量。它在C ++ 11中的Clang中无效,但是与其他编译器和C ++ 98一样无效。






再次更新:



似乎矢量工作正常。但是列表失败

  class Node {
std :: list< Node>儿童;
};






再次更新:



以下是我的代码:

  #include< list& 

using namespace std;

class Node {
list< Node>节点;
};

int main(int argc,char const * argv [])
{
return 0;
}

或更简单:

  #include< list> 

class Node {
std :: list< Node>节点;
};

int main(){}

我使用Clang 4.0和使用以下命令编译:

  clang ++ -std = c ++ 11 -stdlib = libc ++ test.cpp 

错误是

  / usr / bin /../ lib / c ++ / v1 / list:212:9:error:字段有不完整的类型'Node'


解决方案

如果不编译,意味着 vertex 试图使用 Node ,要求它被完全定义。大多数时候,这意味着(对于通用代码)使用 T 参数的大小:




  • 明确( sizeof(T)

  • 或隐式地 template< typename T& ; struct vertex {T data [3]; }; 使用 T 的大小来计算类型的布局



另一个(可能的)问题是依靠 T 的方法来进行模板实例化;



您可以通过更改 vertex 的定义来避免此要求。不知道是什么,我们将无法得到更多的具体...


As this website shows, following code will not be supported in Clang using C++11:

class Node {
    vertex<Node> children;
};

An error will occur:

field has incomplete type 'Node'

But such code is supported in C++98 and other compilers such as gcc in C++11.

I know I can use

vertex<Node*>

instead, but at present I have some incompatibility issue with old code in C++98.

My question is, (1) can I compile such code using Clang in C++11? (2) I think a tree structure does inevitably need definition like above, without support of such feature, how can I realize such tree structure?


update:

Sorry for forgetting to give definition of vertex, What about the following code:

class Node {
    vector<Node> children;
};

Just change vertex into a container vector. It is not valid in Clang with C++11, but ok with other compilers and with C++98.


update again:

It seems vector works OK..but list fails

class Node {
    std::list<Node> children;
};


update again:

Following is my code:

#include <list>

using namespace std;

class Node {
    list<Node> nodes;
};

int main(int argc, char const *argv[])
{
    return 0;
}

or simpler:

#include <list>

class Node {
    std::list<Node> nodes;
};

int main() {}

I'm using Clang 4.0 and using the following command to compile:

clang++ -std=c++11 -stdlib=libc++ test.cpp

The error is

/usr/bin/../lib/c++/v1/list:212:9: error: field has incomplete type 'Node'

解决方案

If it does not compile, it means that vertex attempts to use Node in a way that requires it to be completely defined. Most of the time, this implies (for generic code) using the size of the T parameter:

  • either explicitly (sizeof(T))
  • or implicitly template <typename T> struct vertex { T data[3]; }; is using the size of T to compute the layout of the type

Another (possible) issue, is relying on methods of T for some template instantiation; however this is much rarer.

You can avoid this requirement by changing the definition of vertex. Not knowing what it is though, we won't be able to get much more specific...

这篇关于不完整的类型与Clang编译c ++ 11代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆