0

Here is my problem.

I have a Linked List class as follows:

#include <iostream>
#include "List.h"
template <class Elem>
class LList : public List<Elem> { //List is a virtual base class
protected:

    Node <Elem> *head;
    Node <Elem> *fence;
    Node <Elem> *tail;

    int leftCount; 
    int rightCount;
    void init();
    void removeAll(); 

public:
    LList(); 
    ~LList();
    //Rest of methods overridden from List class
    //////
};

Then I have a class called SortedLList which inherits from LList as follows:

#include "LinkedList.h"
#include "Helper.h"
template <class Elem> 
class SortedLList : public LList<Elem> {
protected:

    Helper *helper;
public:

    SortedLList();
    ~SortedLList();
    bool insert(const Elem&); //Override insertion method from LList class
};

In the implementation of SortedLList (SortledLList.cpp):

#include "SortedLList.h"
template <class Elem>
SortedLList<Elem>::~SortedLList() {
    removeAll();
}

template <class Elem>
bool SortedLList<Elem>::insert(const Elem &_e) {
    fence = head;
    //Rest of Code..
}

I am having a compiler error that says : Use of undeclared identifier removeAll(). Same thing for fence and head pointers are not being recognized. What did I do wrong?

Thank You.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Joe
  • 2,386
  • 1
  • 22
  • 33
  • 2
    Is this an academic exercise or do you have some other reason to be re-implementing the Standard Library? – tadman Sep 25 '13 at 19:00
  • It is an academic exercise in which I have to implement almost every function I may use – Joe Sep 25 '13 at 19:01
  • What do you mean by 'they can't be seen'? Suggest you post the code where you're instantiating SortedLList and calling an inherited method & the compiler error(s). – Mike Makuch Sep 25 '13 at 19:03
  • 1
    "cannot be seen", can you 1. Give code which gives error, 2. copy-paste the error, 3. indicate which line in the question code the error refers to. – hyde Sep 25 '13 at 19:03
  • 1
    Should you be using private inheritance for 'SortedLList is implemented in terms of LList'? Public inheritance says 'a SortedLList _is a_ LList', so every method that applies to LList can be applied to SortedLList; further, you can pass a reference to a SortedLList to a function expecting an LList, and it will work fine. Are you sure that's what you're going to achieve? If not, you probably need to use something other than public inheritance. – Jonathan Leffler Sep 25 '13 at 19:05
  • Sure let me edit the code to show where the error is – Joe Sep 25 '13 at 19:06
  • interesting... http://ideone.com/u2wj6x Anyone posit why this doesn't work? If you remove the template argument it works as expected. It _does_ compile and run on VS2010. – Chad Sep 25 '13 at 19:10
  • Anyone? I am using the latest version of XCode by the way. – Joe Sep 25 '13 at 19:18
  • Are you #including the file with the implementation or compiling it as a separate translation unit? – Thomas Matthews Sep 25 '13 at 19:26
  • "In the implementation of SortedLList".. Where is that implementation, and what makes you believe it has access to `removeAll()` ? The posted code has no indication you're including the correct template decls and defs whatsoever. And since you've chosen not to provide the *full* base of related code (half the classes used in this post are not *inlcuded* in this post: `Node<>`, `List`, `Helper` are all fictional) all you're going to get are half-blind guesses. Don't post half-code. Clearly identify each header involved, each source file involved, and include them in the post. – WhozCraig Sep 25 '13 at 19:38
  • @WhozCraig Helper class is totally irrelevant here. As mentioned in the comment List is an abstract base class whose methods are implemented in LList. Again irrelevant here. The issue is when I created SortedLList which inherits from LList. The properties are not seen. – Joe Sep 25 '13 at 19:55
  • Functional-relavance aside, since their lack of presence inhibits compiling the posted code and seeing the behavior triggering your reasons for posting in the first place, "irrelevant" in regards to *reproducing* your issue is not accurate. If you can pair down the source to *just* the issue and it is copy/paste/repro applicable, by all means, please do. I.e. `Helper` isn't relevant, then get rid of it and pair-down to what *is*. And Note: [Don't implement templates in .cpp files](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – WhozCraig Sep 25 '13 at 19:58
  • Code that you have posted looks OK. We cannot know for sure because we cannot compile it and see. – n. m. could be an AI Sep 25 '13 at 20:02
  • I threw in dummy `Node` and `List` template classes, threw out the `Helper` class and member var, included everything in one source file, and it compiles. The only thing that stops me from calling `ll.removeAll()` on an instance of `SortedLList ll;` is the protection you've established on it (i.e. you declared it protected). But it *is* found, so I think your code is ok (at least in spirit =P). I am concerned you're implementing your templates in .cpp files. The link I posted in prior comment will tell you why that is almost-never the correct way to do it. Wishing you luck. – WhozCraig Sep 25 '13 at 20:08

1 Answers1

2

Because your class is a template there are certain issues that can happen to confuse the compiler. You may think your code is straight forward and easy to understand, and in this case it is. Older compilers used to do their best to guess and compile this code.

However, newer compilers are more strict and fail on all versions of this type of code, in order to prevent programmers from relying on it.

What you need to do is use the this pointer when calling base class functions. That makes the call unambiguous and clear. That would look like this->removeAll().

Another option would be to use a full name qualification like LList<Elem>::removeAll(). I prefer using this because it is easier to read.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Linking another question that bears on the topic: http://stackoverflow.com/questions/10639053/name-lookups-in-c-templates – Zan Lynx Sep 26 '13 at 00:52