an odd problem with a template class

This is a discussion on an odd problem with a template class within the C++ Programming forums, part of the General Programming Boards category; I've been working with templated classes for years now...but we all have our brain farts, dont we? So...I was simply ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    an odd problem with a template class

    I've been working with templated classes for years now...but we all have our brain farts, dont we? So...I was simply building a key-value map class, using a binary search tree as my backbone for the class.

    Anyways, I am receiving some odd errors when I try to compile...and after doing an examination of my code I don't know what is wrong. The errors are as follows:

    1. MAP_DTP is not a template type (line 8)
    2. MAP_DTP is not a template (line 21)

    The are more...but the basically follow the same pattern as those first 3. There is actually one more that I find quite funny. I will post it as comic relief:

    3. confused by earlier errors, bailing out

    haha...i love that...i made the computer confused.

    Anyways...the code for the class declaration itself is quite short, so I shall post it:

    Code:
    #ifndef __MAP_DTP_H
    #define __MAP_DTP_H
    
    #include "BST.h" //where BST is contained
    #include "sort240.h" //where simpleVector is contained
    
    template <class TKey, class TValue>
    class MAP_DTP
    {
    
    protected:
        
        //The actual binary-search-tree containing the keys and values
        BST myTreeMap;
        
        void OutputMap ( ostream & output, BSTNode *root );
        
    public:
    
        //Constructors
        MAP_DTP ( );
        MAP_DTP ( const MAP_DTP<TKey, TValue> & otherMap );
        
        //Destructors
        ~MAP_DTP ( );
        
        //Accessors
        bool ContainsKey ( TKey myKey );
        bool ConstainsValue ( TValue myValue );
        bool IsEmpty ( void );
        
        int Size ( void );
        
        TValue & GetValue ( TKey myKey );    
        
        simpleVector <TKey> GetKeys ( void );
        simpleVector <TValue> GetValues ( void );
        
        void Test ( ostream & output );
        
        //Modifiers
        void Clear ( void );
        bool SetValue ( TKey myKey, TValue myValue );
        bool RemoveKey ( TKey myKey );
        
        //Operators
        TValue & operator [] ( const TKey & index );
        bool operator == ( const MAP_DTP<TKey, TValue> & rhs );
        MAP_DTP<TKey, TValue> & operator = ( const MAP_DTP<TKey, TValue> & rhs );
    
    };
    
    #endif /* __MAP_DTP_H */
    Do you guys see anything that might be wrong in my syntax or logic? Thanks for any help.
    Last edited by DavidP; 10-31-2006 at 11:25 PM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you comment out #21 ( MAP_DTP ( const MAP_DTP<TKey, TValue> & otherMap ); ) does it work? im reaching here...

    edit: could you post a link to the other 2 files so i could test on my machine and try and debug in the morning?

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    here i will go ahead an upload all the files.

    my driver program is really simple:

    Code:
    #include <iostream>
    #include <string>
    #include "mapdtp.h"
    
    using namespace std;
    
    int main ( void )
    {
        MAP_DTP <string, string> myMap;
        
        return 0;    
    }
    I didn't upload it, because you can only upload 5 files. :-)

    But the 5 files that I did upload are all that are needed.

    sort240 - contains simpleVector (used in the program) and MATRIX_DTP (not used)
    bst.h - contains the Binary Search Tree class declaration
    bst.cpp - contains the class definition
    mapdtp.h - contains the MAP_DTP class declaration
    mapdtp.cpp - contains the class definition
    Attached Files Attached Files
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    what compiler do you use?

    and what happens if you put the member functions in the class declaration?
    I remember you have to do:

    template<class T>
    void X<T>::Print(const T &t)
    {
    }

    if you put the body inside a cpp file
    Last edited by Laserve; 11-01-2006 at 12:40 AM.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    i am using devc++ as my IDE, which uses g++ as the compiler.
    My Website

    "Circular logic is good because it is."

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In BST_Node and BST, you have the declaration
    friend class BST;

    This makes the compiler think BST is a non-template class, confusing it when encountering the template.

    I don't know how the syntax goes to make all template instantiations a friend, but IMO the design is screwed anyway - the generic underlying tree should not even know about the map implemented on top of it, much less make it a friend.
    Then there's the problem that, the way you have it, your map can only use keys and values implicitely convertible to strings. Pretty useless, if you ask me.
    Oh, and expect linker errors when you finally get it to work. Template implementation in source files and all that stuff.

    A more typical way to implement this is to write a BST template that has only a value-type and a comparison-predicate template argument. It uses the comparison predicate to sort the values.
    Then, the map is implemented by using the BST with a tuple of the key and the value as the value type and supplying a comparison predicate that only compares the keys, ideally according to the predicate supplied to the map by the user.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    >your map can only use keys and values implicitely convertible to strings. Pretty useless, if you ask me.

    Yeah, I know. That is one change I hadn't made yet. The BST class was originally written earlier as a non-template class for an assignment. Then I wanted to build a key-value map, and thought that a BST would be a good way to store the data, so I started modifying the code to support it...but that part wasn't finished yet.

    Needless to say, the fact that at the present moment in time BST only supports strings shouldn't be the cause of compiler errors.

    >This makes the compiler think BST is a non-template class

    BST is a non-template class as of right now.

    Maybe you meant to say MAP_DTP instead of BST? If that is so, then you are correct, and that might be the problem. Maybe I should finish the conversion of BSTNode and BST to templates. I made MAP_DTP a friend to both, wanting it to be able to access the private variables and functions of both.
    My Website

    "Circular logic is good because it is."

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yeah, I meant MAP_DTP, not BST. Remove the friends and the errors go away. (Of course, you have many, many more.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    yeah...so in order to really get everything working right I need to make both BSTNode and BST templated...and it would certainly be a good thing to do, but as I think about it, for the scope of the project that I am working on...I really don't need templates...so I just decided to "de-template-ize" MAP_DTP and only use strings as key-value pairs... Now the MAP_DTP class is working perfectly... In the future I will templatize all of it when needed...
    My Website

    "Circular logic is good because it is."

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Wait a moment, you're not just using this as an exercise for templates and BSTs?

    Why don't you use std::map?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    im using it both as an exercise to refresh myself a bit, and also as part of a project I am working on... can't use std::map because the professor wont let us for this specific project, otherwise i would have done that a long time ago.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  3. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM
  4. Template problem
    By LilShieste in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2002, 12:17 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21