Signature of a template method that returns a nested template typegives compile errors

Discussion in 'C++' started by kurt krueckeberg, Apr 29, 2013.

  1. I have template class with a nested template class like this

    #include <utility>
    template<typename K> class Tree23 {

    public:
    template<typename Key> class Node {
    private:
    friend class Tree23<K>;
    // snip . . .
    };
    Node<K> root;
    public:
    // snip ...
    std::pair<bool, Node<K> *> Search(K key);
    };

    I get several compile errors on the signature of the Search method implementation

    template<typename K>
    std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
    {
    // make sure tree has at least one element
    if (root == 0) {
    return std::make_pair(false, 0);

    } else {
    return Locate(key, root);
    }
    }


    The errors correspond to the line

    template<typename K> std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)

    The complie errors are:

    Node.h:64:55: error: type/value mismatch at argument 2 in template parameter list for 'template<class _T1, class _T2> struct std::pair'
    Node.h:64:55: error: expected a type, got '(Tree23<K>::Node < <expression error>)'
    Node.h:64:58: error: expected unqualified-id before '>' token
    Node.h:64:58: error: expected initializer before '>' token

    I also tried adding typename to the signature

    template<typename K>
    std::pair<bool, typename Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
    { //...}

    This gave a different set of errors

    Node.h:57:48: error: 'typename Tree23<K>::Node' names 'template<class K> template<class Key> class Tree23<K>::Node', which is not a type
    Node.h:64:67: error: template argument 2 is invalid
    Node.h:64:70: error: prototype for 'int Tree23<K>::Search(K)' does not match any in class 'Tree23<K>'
    Node.h:57:53: error: candidate is: std::pair<bool, Tree23<K>::Node<K>*> Tree23<K>::Search(K)

    Why, with typename added, does it think the prototype is 'int Tree23<K>::Search(K)'
    kurt krueckeberg, Apr 29, 2013
    #1
    1. Advertising

  2. Re: Signature of a template method that returns a nested templatetype gives compile errors

    On 4/29/2013 12:41 PM, kurt krueckeberg wrote:
    > I have template class with a nested template class like this
    >
    > #include <utility>
    > template<typename K> class Tree23 {
    >
    > public:
    > template<typename Key> class Node {
    > private:
    > friend class Tree23<K>;
    > // snip . . .
    > };
    > Node<K> root;
    > public:
    > // snip ...
    > std::pair<bool, Node<K> *> Search(K key);
    > };
    >
    > I get several compile errors on the signature of the Search method implementation
    >
    > template<typename K>
    > std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
    > {
    > // make sure tree has at least one element
    > if (root == 0) {
    > return std::make_pair(false, 0);


    This one will attempt to generate a template with 'bool,int' as its
    arguments, not with 'bool, Tree23...' as you want. You need to
    explicitly cast the null to your value type. Better yet, create the
    typedefs for the types of 'value' and 'key' in your class. Take a hint
    from standard containers (see the 'value_type' etc. typedefs)

    >
    > } else {
    > return Locate(key, root);


    'Locate' is undeclared here.

    > }
    > }
    >
    >
    > The errors correspond to the line
    >
    > template<typename K> std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
    >
    > The complie errors are:
    >
    > Node.h:64:55: error: type/value mismatch at argument 2 in template parameter list for 'template<class _T1, class _T2> struct std::pair'
    > Node.h:64:55: error: expected a type, got '(Tree23<K>::Node < <expression error>)'
    > Node.h:64:58: error: expected unqualified-id before '>' token
    > Node.h:64:58: error: expected initializer before '>' token
    >
    > I also tried adding typename to the signature
    >
    > template<typename K>
    > std::pair<bool, typename Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
    > { //...}
    >
    > This gave a different set of errors
    >
    > Node.h:57:48: error: 'typename Tree23<K>::Node' names 'template<class K> template<class Key> class Tree23<K>::Node', which is not a type
    > Node.h:64:67: error: template argument 2 is invalid
    > Node.h:64:70: error: prototype for 'int Tree23<K>::Search(K)' does not match any in class 'Tree23<K>'
    > Node.h:57:53: error: candidate is: std::pair<bool, Tree23<K>::Node<K>*> Tree23<K>::Search(K)
    >
    > Why, with typename added, does it think the prototype is 'int Tree23<K>::Search(K)'


    Tree23<K>::Node is not a type, you can't use it after 'typename'.
    '...Node' is actually a template-id, it needs template arguments to
    become a *type*.

    Post a short *complete* example so we could copy-and-paste it to compile
    *and edit* if you really want our help.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 29, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,045
    Joerg Jooss
    Apr 24, 2004
  2. sqweek
    Replies:
    2
    Views:
    391
    Charles Rapp
    Jan 13, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    837
    Lionel B
    Mar 1, 2007
  4. lyallex
    Replies:
    6
    Views:
    523
    lyallex
    Sep 29, 2007
  5. Gustavo Narea
    Replies:
    14
    Views:
    847
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page