www.digitalmars.com         C & C++   DMDScript  

c++ - Problem with a template

reply Edward A. Waugh <edward_waugh hotmail.com> writes:
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
Sep 28 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
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.
Sep 30 2006
parent reply Edward Waugh <edward_waugh hotmail.com> writes:
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
Oct 01 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
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.
Oct 01 2006
parent reply Edward A. Waugh <edward_waugh hotmail.com> writes:
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
Oct 03 2006
parent Walter Bright <newshound digitalmars.com> writes:
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>.
Oct 03 2006