Thread: Noob question about templates & inheritance

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    20
    Hi all,

    I've got a noob question to post. I've got this class named "Vector":

    Code:
    using namespace std;
    
    
    
    template<class T> class Vector;
    
    
    
    template<class T>
    
    ostream& operator<<(ostream&, const Vector<T>&);
    
    
    
    template <class T>
    
    class Vector
    {
    
          friend ostream& operator<< <>(ostream&, const Vector<T>&);
    
    protected:
    
          T* data;
    
          unsigned len;
    
    public:
    
          Vector(unsigned = 10);
    
          Vector(const Vector<T>&);
    
          virtual ~Vector(void);
    
          Vector<T>& operator =(const Vector<T>&);
    
          bool operator==(const Vector<T>&);
    
          T& operator [](unsigned);
    
    	  unsigned getLength(void) {return len;}
          (...)
    };
    I've also got a class named "AssociativeArrayInheritance" that descends form Vector, as you can see:

    Code:
    template<class KeyType, class ValueType>
    
    class AssociativeArrayInheritance : public Vector<Pair<KeyType, ValueType> >
    
    {
    
    public:
    
    	AssociativeArrayInheritance(unsigned size=0):Vector<Pair<KeyType, ValueType> >(size){}
    
    	ValueType& operator [](const KeyType);
    
    };
    
    
    
    template<class KeyType, class ValueType>
    
    ValueType& AssociativeArrayInheritance<KeyType, ValueType>::operator [](const KeyType key)
    
    {
    	unsigned i = Vector::getLength (); // HOW CAN I CALL THIS METHOD BY INHERITANCE? :| iCAN'T SEEM TO FIGURE OUT...
           (...)
    When compiling this file, the last line (line163) ("unsigned i = Vector::getLength ();") is getting me the following error:
    (all code is in file "vector2.h")
    vector2.h: In member function ‘ValueType& AssociativeArrayInheritance<KeyType, ValueType>:perator[](KeyType)’:
    vector2.h:163: error: ‘template<class T> class Vector’ used without template parameters
    Can anybody help me? I know it must be something stupid & basic but I've searched the web, talked with colleagues, and by now I should be delivering this to S teacher...
    :
    Last edited by blacknail; 10-24-2008 at 05:51 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the error seems pretty straightforward -- you have a Vector, but it's a Vector of ... what? KeyTypes? ValueTypes? Something Elses? How do I know? And how do you expect to call getLength without an object? You have to have a specific Vector in mind to call a member function on it.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    20
    You have to have a specific Vector in mind to call a member function on it.
    With inheritance do I have to create an instance of the class to call a method in the super class?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If it's a non-static method, yes. All methods need an object to operate on.

    But do you mean, "do I need a separate instance of the base class to call base class methods from within the derived class?" The answer to that question would be no. If you have permission to execute the method in the base class, you can call it from the derived class just by itself.
    Code:
    class base {
    public:
        void base_function() {}
    };
    
    class derived : public base {
    public:
        void derived_function() {
            base_function();
        }
    };
    [edit] Can you tell I didn't read the rest of the thread? Sorry for this rather useless post. [/edit]
    Last edited by dwks; 10-24-2008 at 06:03 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay, I missed that you wanted the inherited version. So then it's Vector<Pair<KeyType,ValueType> >::getLength.

    Is there a reason you need the base version and not the inherited version? (Did you override it, and now need the original?)

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    20
    In first place, thanks to all. I was in the verge of despair and now I'm getting better.

    Second, tabstop, I only have getLength() defined in the superclass (Vector) and I want to call it in sub class (AssociativeArrayInheritance). As simple as that!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then do so. Everything in Vector is in AssociativeArrayInheritance.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    20
    That was just what I was thinking. I just thought that I could call the method by just typing getLength() in the subclass :S

    Anyway, thanks for all!

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You must type "this->getLength()". This is a rather complicated aspect of the name lookup rules in the face of templates. The C++ FAQ Lite describes why.
    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

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't forget to make getLength a const method as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. super noob question
    By verbity in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2007, 11:38 AM
  2. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  3. Inheritance question.
    By antex in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2006, 09:59 AM
  4. Replies: 5
    Last Post: 11-01-2002, 06:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

Tags for this Thread