1

So I've been a little spoiled with Java/C#, and have pretty much (apparently) forgotten how to program in C++.

I have a templated doubly-linked list ("Dlist") that I've created. It's laid out in a header file (god, header files are weird), and implemented in a CPP.

So, if I remember what little C++ I know right, a fn in that CPP looks something like this:

//ctor
template <typename T>
Dlist<T>::Dlist()
{
    first = NULL;
    last = NULL;
    return;
}//end fn

I want to create a pair of subclasses to this Dlist. In a new CPP, called, say, "stack.cpp", I have:

#include "dlist.h"

template <typename T>
class Stack : public Dlist<T>
{
  public:
     //template <typename T>
     void add(T *o) //these are virtual fns in dlist
     {
        //will call push
     }

    //template <typename T>
    T * remove() //these are virtual fns in dlist
    {
        //will call pop
    }

    //template <typename T>
    Stack()
    {
        //i have NO idea what to put in here
    }

};//end class

But when I have the line

 Stack <double> stack;

in my int main(), Visual Studio FLIPS OUT.

error C2062: type 'double' unexpected

error C2065: 'Stack' : undeclared identifier

So: how do I properly create, and call, subclasses of a templated class?

I am not allowed to use the cstdlib for my stuff. Yay homework!

Community
  • 1
  • 1
  • If the compiler complains about `double` perhaps it doesn't know that Stack is a template. Did you include stack.h in the main file? – Bo Persson Jan 21 '12 at 05:17
  • It'd probably be best to pick up a good book and get better acquainted with C++ in general. You wouldn't really use pointers, or make assignments in a constructor, or `return` from a constructor in idiomatic C++. – Kerrek SB Jan 21 '12 at 05:17
  • I don't have a stack.h -- it seemed unnecessary. Do I really have to break EVERY single one of these subclasses (and any future subclasses) into two files -- a loosely defined header and a implemented cpp? – Cory Gordinier Jan 21 '12 at 05:18
  • OP says he is putting the Stack definition in stack.cpp, not stack.h, perhaps this is the problem. – MtnViewJohn Jan 21 '12 at 05:24
  • Is your `int main()` in `stack.cpp`, or in some other file? In C++, any uses of the class need to be in a context where the class is declared. – Brooks Moses Jan 21 '12 at 05:26
  • int main() is currently in main.cpp. And what MtnViewJohn said is correct; Stack's definition has no related header. – Cory Gordinier Jan 21 '12 at 05:27
  • Template classes are typically fully implemented in the .h file and have no .cpp file. See [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – MtnViewJohn Jan 21 '12 at 05:29
  • Wouldn't that mean that having a dlist.cpp is bad, and it should all be in dlist.h? – Cory Gordinier Jan 21 '12 at 05:34
  • Yes, all your template classes should only have .h files. – MtnViewJohn Jan 21 '12 at 05:40

1 Answers1

1

Actually you don't need the template on remove function. Try removing it. Also, when working with templated classes, you'll most likely to implement most functions directly into the header, so compiler will know how to generate the method depending on the type you pass.

Here's the code:

Dlist:

template<typename T>
class Dlist
{
private:
    T* first;
    T* last;

public:
    Dlist()
    {
        this->first = NULL;
        this->last = NULL;
    }

    T* remove() 
    { 
        return NULL; 
    }

    void add(T* obj) 
    {
        // add algorithm
    }
};

Stack:

template<typename T>
class Stack : protected Dlist<T>
{
public:
    Stack()
        : Dlist<T>() // if you need to call constructor code from the base
    {
    }

    void push(T* obj)
    {
        this->add(obj);
    }

    T* pop()
    {
        return this->remove();
    }
};

Declaration:

Stack<double> stack;

Note at this line:

class Stack : protected Dlist<T>

Its protected because if you want a stack, and inherits Dlist as public, you'll still have a Dlist with Stack functions.

WoLfulus
  • 1,948
  • 1
  • 14
  • 21