本文介绍了Boost容器无法使用未定义(但已声明)的类进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法在MSVStudio 2010 Express中编译,这似乎是因为boost容器声明创建了所包含类型的(静态?)实例.将boost::ptr_list<TypeContained>更改为std::list<TypeContained *>会使它成功编译,但是我喜欢boost容器.有人知道我该如何解决吗?错误是error C2504: 'Proxy<TypeContainer,TypeContained>' : base class undefined

The following code fails to compile in MSVStudio 2010 Express, and seems to be because the boost container declaration creates a (static?) instance of the contained type. Changing boost::ptr_list<TypeContained> to std::list<TypeContained *> causes it to compile successfully, but I like the the boost containers. Anyone have an idea how I can get around this? The error is error C2504: 'Proxy<TypeContainer,TypeContained>' : base class undefined

#include <string>
#include <boost/ptr_container/ptr_list.hpp>

template <typename TypeContainer, typename TypeContained>
class Proxy
{
private:
    typename boost::ptr_list<TypeContained>::iterator m_clsPosition;

public:
    class Container {};
};

template <typename V> class Container;

template <typename V>
class Dependent : public Proxy<Container<V>, Dependent<V> >,
                  public V {};

template <typename V>
class Container : public Proxy<Container<V>, Dependent<V> >::Container {};

int main(int argc, char * argv[])
{
    Container<std::string> clsContainer;
    return 0;
}

推荐答案

因此,使用clang进行编译时,问题出在试图在TypeContainer上使用sizeof时出现了问题,但目前尚未确定TypeContainer的大小是多少. ,因为您仍在定义它.

so, compiling with clang, the problem lies in boost trying to use sizeof on TypeContainer, but at this point, it's not yet determined what the size of TypeContainer is, since you're still defining it.

从根本上讲,根本问题是

So, the root problem, as a simpler case:

template <typename A>
class T {
    static const int a = sizeof(T<A>);
};

int main() {
    T<int> d;
}

换句话说,调用sizeof会创建一个循环依赖关系,但是使用指针(具有已知大小)就永远不会创建该依赖关系.

In other words, there's a loop dependency created by invoking sizeof, but using a pointer (which has a known size), this dependency is never created.

这篇关于Boost容器无法使用未定义(但已声明)的类进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:12