c++ - 模板模板参数 - 编译错误

标签 c++ templates

我尝试从 David Vandevoorde 和 Nicolai M. Josuttis 的“C++ 模板 - 完整指南”中编译以下代码示例:

    #include <iostream>
    #include <deque>
    #include <vector>
    #include <stdexcept>
    #include <memory>

    template < typename T, 
               template < typename ELEM, typename = std::allocator< ELEM > > 
                          class CONT = std::deque >
    class ourstack
    {
    private:
        CONT< T > elems;
    public:
        void push( T const& );
        void pop();
        T top() const;
        bool empty() const
        { return elems.empty(); }
        template < typename T2 >
                template < typename ELEM2, 
                           typename = std::allocator< ELEM2 > > class CONT2 >
        ourstack< T, CONT >& operator = ( ourstack< T2, CONT2 > const & );
    };

    template < typename T, 
               template < typename, typename > class CONT >
    void ourstack< T, CONT>::push( T const& elem )
    {
        elems.push_back( elem );
    }

    template < typename T, 
               template < typename, typename > class CONT >
    void ourstack< T, CONT>::pop()
    {
        if ( elems.empty() )
        {
            throw std::out_of_range( "Stack empty" );
        }
        elems.pop_back();
    }

    template < typename T, 
               template < typename, typename > class CONT >
    void ourstack< T, CONT>::top() const
    {
        if ( elems.empty() )
        {
            throw std::out_of_range( "Stack empty" );
        }
        return elems.back();
    }

    template < typename T, 
               template < typename, typename > class CONT >
        template < typename T2 >
                   template < typename, typename > class CONT2 >
    ourstack< T, CONT >& ourstack< T, CONT >::operator = ( ourstack< T2, CONT2 > const & op2    )
    {
        if (( void*) this == (void*) op2 )
        {
            return *this;
        }
        ourstack< T2 > tmp( op2 );
        elems.clear();
        while ( !tmp.empty() )
        {
            elems.push_front( tmp.top() );
            tmp.pop();
        }
        return *this;
    }

    int main( int argc, char* argv[] )
    {
        ourstack< int > s;
        return 0;
    }

但是

我使用的是 gcc 4.4.3 版。

编译器写入信息:

    g++ -Wall template_of_template.cpp -o template_of_template 
    template_of_template.cpp:22: error: too many template-parameter-lists
template_of_template.cpp:22: error: expected unqualified-id before ‘>’ token
template_of_template.cpp:46: error: prototype for ‘void ourstack<T, CONT>::top() const’ does not match any in class ‘ourstack<T, CONT>’
template_of_template.cpp:17: error: candidate is: T ourstack<T, CONT>::top() const
template_of_template.cpp:58: error: too many template-parameter-lists
template_of_template.cpp:58: error: expected unqualified-id before ‘>’ token

有什么问题?

最佳答案

代码的唯一问题是您需要学习更准确地打字。 ;-]

在修复了多个拼写错误(t 而不是 r> 而不是 ,)之后,这里是处于可编译状态的相同代码:

#include <iostream>
#include <deque>
#include <vector>
#include <stdexcept>
#include <memory>

template < typename T, 
           template < typename ELEM,
                      typename = std::allocator< ELEM > > class CONT = std::deque >
class ourstack
{
private:
    CONT< T > elems;
public:
    void push( T const& );
    void pop();
    T top() const;

    bool empty() const
    { return elems.empty(); }

    template < typename T2,
               template < typename ELEM2,
                          typename = std::allocator< ELEM2 > > class CONT2 >
    ourstack< T, CONT >& operator = ( ourstack< T2, CONT2 > const & );
};

template < typename T, 
           template < typename, typename > class CONT >
void ourstack< T, CONT>::push( T const& elem )
{
    elems.push_back( elem );
}

template < typename T, 
           template < typename, typename > class CONT >
void ourstack< T, CONT>::pop()
{
    if ( elems.empty() )
    {
        throw std::out_of_range( "Stack empty" );
    }
    elems.pop_back();
}

template < typename T, 
           template < typename, typename > class CONT >
T ourstack< T, CONT>::top() const
{
    if ( elems.empty() )
    {
        throw std::out_of_range( "Stack empty" );
    }
    return elems.back();
}

template < typename T, 
           template < typename, typename > class CONT >
template < typename T2,
           template < typename, typename > class CONT2 >
ourstack< T, CONT >& ourstack< T, CONT >::operator = ( ourstack< T2, CONT2 > const & op2    )
{
    if (( void*) this == (void*) op2 )
    {
        return *this;
    }
    ourstack< T2 > tmp( op2 );
    elems.clear();
    while ( !tmp.empty() )
    {
        elems.push_front( tmp.top() );
        tmp.pop();
    }
    return *this;
}

int main( int argc, char* argv[] )
{
    ourstack< int > s;
    return 0;
}

关于c++ - 模板模板参数 - 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7959288/

相关文章:

c++ - 从 C++ 文件中解析目录位置

c++ - 字符串到字符 - C++

c++ - 将地址转换为函数 : using "void * void "

c++ - 当存在用户定义的移动分配运算符时,模板化的移动分配运算符被删除

c++ - IGraphicsCaptureItemInterop.CreateForWindow遇到winrt::hresult_access_denied?

c++ - 如何防止在 argv 中丢失双引号?

c++ - 使用 std::pair 作为 operator= 的参数时出现编译问题

c++ - 如何将不同模板参数值但相同类型的值放入此类型的变量中? C++

Perl:网站模板集合

c++ - 为模板参数推断层次结构中的最低类型