template problem in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pompom
    New Member
    • Dec 2007
    • 8

    #1

    template problem in c++

    hello all,

    first of all I'd like to say i really like the site, it has helped me a lot these last weeks. i'm a big fan :-)

    but here's the problem I've got:

    i've made an AVL tree using template for the type of the Key and the Value (in the nodes).
    then I initialized it, like this:

    AVLTree<std::st ring, int> avl_common;

    so just an AVL tree with key type => string and
    value type=> int

    then I had to pass it as reference to a function that reads the input from cin and puts every inerted word into the AVL tree, with as value: the number of times that word has occured in the input text.

    this is were it blocks.

    inlees(test, avl_common);

    inlees= read in function and test is a text document and avl_common is the AVL where we're gonna insert them into.

    the function 'inlees' looks like this:

    template <typename Key, typename Value>

    int inlees (ifstream& path, AVLTree<Key, Value>& l);

    _

    but when compiling, it says: (using g++)

    /tmp/ccW1N4R5.o: In function `main':
    main.cc:(.text+ 0xdb): undefined reference to `int inlees<std::bas ic_string<char, std::char_trait s<char>, std::allocator< char> >, int>(std::basic _ifstream<char, std::char_trait s<char> >&, AVLTree<std::ba sic_string<char , std::char_trait s<char>, std::allocator< char> >, int>&)'


    so it doesnt seem it can do the matching between Key and string AND Value and int.
    can anyone tell me here my problem is?

    thx you very much!!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Try calling the function using template parameters:

    [CODE=cpp]inlees<std::str ing, int>(test, avl_common);[/CODE]

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by pompom
      the function 'inlees' looks like this:

      template <typename Key, typename Value>

      int inlees (ifstream& path, AVLTree<Key, Value>& l);
      I hope your function template doesn't look like this because this is not a valid template. That is, there's no function definition.

      Comment

      • pompom
        New Member
        • Dec 2007
        • 8

        #4
        No, i copied that from the header file.

        the thing is: i have an avl that supports templates.
        and after defining a concrete AVL like:
        AVLTree<std::st ring, int> avl_common;

        I would like to pass it on to a read in function.
        that 'inlees' function expects an AVL, but as it is not sure of what type its key and value will be, I've put 2 template type parameters there, Key and Value, like :

        template <typename Key, typename Value>
        int inlees (ifstream& path, AVLTree<Key, Value>& l) {
        ...

        hoping, when calling that function, it will determine the Key and Value type of the defined avl-tree, here called avl_common.

        But unfortunately, it seems it gives an error saying it cant find such function, eg it appearently cant do the matching between Key and string and Value and int.

        Comment

        • pompom
          New Member
          • Dec 2007
          • 8

          #5
          ok I forgot to put em in the header file only, so now those errors are gone.
          now i only get errors for the fact that it doenst seem the find the correct iterator for my
          typename multimap<Key, Value>::iterato r i;

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Maybe you could post that code.

            Comment

            • pompom
              New Member
              • Dec 2007
              • 8

              #7
              got it to work 2 mins after i entered the previous post :-)

              sorry for all the dumb mistakes :D thx all for your quick response

              Comment

              Working...