Thread overview
Problem with a template
Sep 29, 2006
Edward A. Waugh
Sep 30, 2006
Walter Bright
Oct 02, 2006
Edward Waugh
Oct 02, 2006
Walter Bright
Oct 03, 2006
Edward A. Waugh
Oct 03, 2006
Walter Bright
September 29, 2006
Trying out a simple template in order to learn them and I
have encountered a problem.  If you put these 2 files in
the same directory and do "dmc -cpp -I. stackDemo.cpp" you
will get a working stackDemo executable.  But if you
try "dmc -cpp -I. stackDemo.cpp -DSEPARATE" you get

stack.h(88) : Error: 'operator <<' is not a member
of 'stack<int >'
--- errorlevel 1

instead.  For some reason I can't get things to compile if the definition of << is out of the class declaration.  What I am doing wrong?

Also, I would like to know if the declarations for the copy constructor and assignment operator are correct:

   stack(const stack &s);
   stack & operator=(const stack &s);

Should I be using stack or stack<T> for the return and argument types?  All of the examples that I have seen in books are for member functions like

    void push(const T &value);
    T pop();

or constructors where this issue does not arise.  Any help would be appreciated.

Thanks,
Edward
September 30, 2006
Edward A. Waugh wrote:
> Trying out a simple template in order to learn them and I
> have encountered a problem.  If you put these 2 files in
> the same directory and do "dmc -cpp -I. stackDemo.cpp" you
> will get a working stackDemo executable.  But if you
> try "dmc -cpp -I. stackDemo.cpp -DSEPARATE" you get
> 
> stack.h(88) : Error: 'operator <<' is not a member
> of 'stack<int >'
> --- errorlevel 1
> 
> instead.  For some reason I can't get things to compile if
> the definition of << is out of the class declaration.  What
> I am doing wrong?

The thing to try is to produce the smallest possible source file that reproduces the problem. Often, this makes it clear where the trouble is coming from, and it usually isn't obvious when there's a lot of irrelevant source code hiding it.
October 02, 2006
As requested I have simplified the code - please see the attached files - which yield the error:

C:\Upload> dmcpp problem.cpp
problem.h(25) : Error: 'operator <<' is not a member
of 'stack<int >'
--- errorlevel 1

What is wrong?

Thanks,
Edward
October 02, 2006
Edward Waugh wrote:
> As requested I have simplified the code - please see the
> attached files - which yield the error:
> 
> C:\Upload> dmcpp problem.cpp
> problem.h(25) : Error: 'operator <<' is not a member
> of 'stack<int >'
> --- errorlevel 1
> 
> What is wrong?

operator<< is declared as a friend of stack, not as a member.
October 03, 2006
But isn't the << operator supposed to be declared as a friend of the class that its implementing I/O for?

It works if I define it, as a friend, within the class declaration so from now on that is what I am going to do.

 - Edward
October 03, 2006
Edward A. Waugh wrote:
> But isn't the << operator supposed to be declared as a
> friend of the class that its implementing I/O for?

It's declared as a friend, but defined as a member.

You should also switch from <iostream.h>, which is obsolete, to <iostream>.