Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g++;继承器类中的typedef模板_C++_Templates_Inheritance_Typedef - Fatal编程技术网

C++ g++;继承器类中的typedef模板

C++ g++;继承器类中的typedef模板,c++,templates,inheritance,typedef,C++,Templates,Inheritance,Typedef,简化我的问题,我们可以考虑: template <class T> class Base{ typedef typename std::pair<T, T> pair; }; template <class T> class Inheritor : public Base<T> { pair *p; // mean that we want to use construc

简化我的问题,我们可以考虑:

template <class T>
class Base{
    typedef typename std::pair<T, T> pair;
};

template <class T>
class Inheritor : public Base<T> {
    pair *p;                          
    // mean that we want to use constructor of std::pair.
    // say: std::pair withou argument list

    Inheritor<T>::pair *p;
    // dont see his typename
    // say: pair does not name a type

    typename pair *p;
    // I was sure that it works.
    // I dont know why it doesnt work.
    // say: expected nested-name-specifier before 'pair

    typename Inheritor<T>::pair *p;
    // ok!
};
模板
阶级基础{
typedef typename std::pair pair;
};
模板
类继承器:公共基{
对*p;
//这意味着我们要使用std::pair的构造函数。
//say:std::pair withou参数列表
继承者::pair*p;
//我看不到他的打字名
//say:pair不命名类型
类型名对*p;
//我确信它是有效的。
//我不知道为什么它不起作用。
//say:在“pair”之前需要嵌套名称说明符
typename继承器::pair*p;
//好的!
};
为什么我们不能写打字对*p?我不明白继承人的理由:!这会使代码更复杂,更难阅读

PS (指cource public。正如我所说的“简化我的问题…”)

typedef-typename-Base::pair-pair;
在我看来,这是一个。。。难以翻译的俄语单词

它看起来像是乱七八糟的东西或是管道胶带,或是哈克=)

据我了解:

typedefs继承通常的函数或变量。 但是它是不可访问的(!!!)。 为了获得它,我们应该写

typedef typename Base<T>::pair pair;
typedef-typename-Base::pair-pair;

typedef-typename继承器::pair-pair;

看起来很有趣,但我们需要它!(>当类型名依赖于模板参数时,它是一个依赖名。阅读那篇文章,你会发现使用
typename
没有意义,最后一种情况除外

下面是您的代码可能的外观:

template <class T>
class Base
{
public: // you probably don't want a private typedef
    typedef std::pair<T, T> pair; // typename isn't needed here, this isn't dependent
};

template <class T>
class Inheritor : public Base<T>
{
public: // public again
    typedef typename Base<T>::pair pair; // get the Base's pair type, typedef it

    pair *p; // ah, easy to use
};
模板
阶级基础
{
public://您可能不需要私有的typedef
typedef std::pair pair;//此处不需要typename,这不依赖于
};
模板
类继承器:公共基
{
再次公开
typedef typename Base::pair pair;//获取基的对类型,typedef it
pair*p;//啊,很容易使用
};
typedef typename Inheritor<T>::pair pair;
template <class T>
class Base
{
public: // you probably don't want a private typedef
    typedef std::pair<T, T> pair; // typename isn't needed here, this isn't dependent
};

template <class T>
class Inheritor : public Base<T>
{
public: // public again
    typedef typename Base<T>::pair pair; // get the Base's pair type, typedef it

    pair *p; // ah, easy to use
};