Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:53:40+00:00 2026-05-26T13:53:40+00:00

I try to compile the following code example from C++ Templates – The Complete

  • 0

I try to compile the following code example from “C++ Templates – The Complete Guide” by David Vandevoorde and Nicolai M. Josuttis:

    #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;
    }

But

I’m using gcc version 4.4.3.

The compiler write messages:

    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

What’s the problem ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:53:41+00:00Added an answer on May 26, 2026 at 1:53 pm

    The only problem with the code is that you need to learn to type more accurately. ;-]

    After fixing multiple typos (t instead of r, > instead of ,), here is the same code in a compilable state:

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to compile the example code given below, I get the following
I have the following error when I try to compile my code in g+
I try to compile code, that beggins with: #include<stdlib.h> #include<GL/gl.h> #include<glaux.h> with command: cc
I have the following contrived example (coming from real code): template <class T> class
I get the following error when I try to compile an asp.net site using
When I try to compile Gforth 0.7.0, I get the following error: $ ./configure
Sometimes when i try to build/compile a downloaded source, i get following warning: ld:
When I try to compile code on VS 2005 and it fails, the line
When I try to compile this code: struct BasicVertexProperties { Vect3Df position; }; struct
the following code is an example of something I'm trying to do in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.