C ++模板编译错误 - 递归类型或函数依赖 [英] C++ template compilation error - recursive type or function dependency

查看:744
本文介绍了C ++模板编译错误 - 递归类型或函数依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个给出编译错误的模板类。

I wrote a template class which is giving compilation error

template<class T>
class Entity
{
    string EntityName;
    int  EntitySize;
    Entity<T*> pPrev;
    Entity<T*> pNext;
public:
    Entity<T>(const string & name, int size)
    {
        EntityName = name;
        EntitySize = size;
    }
    //member functions
};

我使用MSVC ++ 2008,错误是:

I am using MSVC++ 2008, and the error is :


致命错误C1202:递归类型或
函数依赖上下文太
复杂

fatal error C1202: recursive type or function dependency context too complex

我没有在我的类中写任何递归函数。那么为什么会出现这个错误呢?请帮助。

I have not written any recursive function in my class. Then why this error? Please help.

推荐答案

好的。我解释你面对的问题。但第一件事。您说:

Alright. I'm explaining you the problem you're facing. But first thing first. You said:


我写了一个模板类,它是
,给出编译错误

I wrote a template class which is giving compilation error

首先,就C ++而言,没有模板类这样的东西,只有一个类模板。读取该短语的方式是类的模板,而不是功能模板,它是功能的模板。再次:类不定义模板,模板定义类(和函数)。*引用这里

First of all, as far as C++ is concerned, there is no such thing as a "template class," there is only a "class template." The way to read that phrase is "a template for a class," as opposed to a "function template," which is "a template for a function." Again: classes do not define templates, templates define classes (and functions).* Quoted from here.

现在,让我们看到错误:

Now, lets see the error:


致命错误C1202:递归类型或
函数依赖上下文太
复合

fatal error C1202: recursive type or function dependency context too complex


$ b b

错误说了这一切。 $ 14.7.1 从标准解释了你的问题的原因很好,给你甚至一个例子,这是非常接近你在做什么。所以我甚至不需要自己写一个单词。这里是 $ 14.7.1

The error says it all. $14.7.1 from the Standard explains the cause of your problem very well, giving you even an example which is very much close to what you're doing. So I don't even need to write a single word of my own. Here is $14.7.1


4
指定对
的限制的数量递归
实例化的总深度
,其中
可能涉及多个模板。实例化中的无限
递归的结果是
未定义。 [示例:

template < class T > class X {
X<T >* p; // OK
X<T*> a; //implicit generation of X<T> requires
         //the implicit instantiation of X<T*> which requires
         //the implicit instantiation of X<T**> which ...
};

-end example]

—end example ]


b $ b

请使用 X< T *>阅读评论。 a ,这也是你的情况。因此,您的问题不是因为递归函数,而是因为类模板的递归实例化,导致从以下行:

Please read the comment with X<T*> a, which is pretty much the case with you too. So your problem is not because of recursive function, it's rather because of recursive instantiation of class template, causing from these lines:

  Entity<T*> pPrev;
  Entity<T*> pNext;

希望它解决了您的问题!

Hope, it solves your problem!

编辑:但我想知道你想用 Entity< T *> pPrev ?它似乎是一个错字,你可能想写 Entity< T> * pPrev 。与 pNext 相同。是这样吗?

EDIT : But I'm wondering what are you trying to achieve with Entity<T*> pPrev? It seems its a typo, and you probably wanted to write Entity<T>* pPrev. Same with pNext. Is that so?

并建议改进设计:使用成员初始化列表,而不是分配。也就是说,以下面的方式写你的构造函数:

And an advice to improve the design : Use Member Initialization list, instead of Assignment. That is, write your constructor the following way,

    Entity<T>(const string & name, int size) : EntityName(name), EntitySize(size)
    {
       //all assignments moved to initialization list.
    }

阅读:为什么我更喜欢使用成员初始化列表?

这篇关于C ++模板编译错误 - 递归类型或函数依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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