c++ - Problem with a template
- Edward A. Waugh (81/81) Sep 28 2006 Trying out a simple template in order to learn them and I
- Walter Bright (5/18) Sep 30 2006 The thing to try is to produce the smallest possible source file that
- Edward Waugh (31/31) Oct 01 2006 As requested I have simplified the code - please see the
- Walter Bright (2/11) Oct 01 2006 operator<< is declared as a friend of stack, not as a member.
- Edward A. Waugh (5/5) Oct 03 2006 But isn't the << operator supposed to be declared as a
- Walter Bright (3/5) Oct 03 2006 It's declared as a friend, but defined as a member.
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 begin 644 stack.h M+R\ 26UP;&5M96YT(&$ 9V5N97)I8R!S=&%C:R!C;&%S<R!U<VEN9R!T96UP M('L-" T*("` ('!U8FQI8SH-" T*("` ('-T86-K*'-I>F5?="!S:7IE(#T M,3`I.PT*("` ('YS=&%C:R I.PT*("` ('-T86-K*&-O;G-T('-T86-K("9S M("` =F]I9"!P=7-H*&-O;G-T(%0 )G9A;'5E*3L-"B` ("!4('!O<" I.PT* M(&9R:65N9"!O<W1R96%M("8 ;W!E<F%T;W(\/"AO<W1R96%M("9O<RP 8V]N M<W0 <W1A8VL )G,I('L-"B` ("` ("` 9F]R("A4("IP='( /2!S+G1O<"`[ M('!T<B`^/2!S+F)A<V4 .R!P='(M+2D >PT*("` ("` ("` ("` ;W, /#P M.PT*("` (%0 *G1O<#L- M96UP;&%T93QC;&%S<R!4/ T*<W1A8VL\5#XZ.G-T86-K*'-I>F5?="!S:7IE M/G1O<"`]('1H:7,M/F)A<V4 +2`Q.PT*("` ('1H:7,M/G-I>F4 /2!S:7IE M=&4\8VQA<W, 5#X-"G-T86-K/%0^.CIS=&%C:RAC;VYS="!S=&%C:SQ4/B`F M=&AI<RT^=&]P(#T M<&5R871O<CTH8V]N<W0 <W1A8VL\5#X )G,I('L-"B` ("!D96QE=&5;72!T M:&ES+3YB87-E.PT*("` ('1H:7,M/F)A<V4 /2!N97< 5%MS+G-I>F5=.PT* M("` (&EN="!E;&5M96YT<R`]("AS+G1O<"`M(',N8F%S92D *R`Q.PT*("` M(&UE;6-P>2 H=F]I9"`J*71H:7,M/F)A<V4L("AV;VED("HI<RYB87-E+"!E M;&5M96YT<RIS:7IE;V8H5"DI.PT*("` ('1H:7,M/G1O<"`]('1H:7,M/F)A M" T*=&5M<&QA=&4\8VQA<W, 5#X-"G9O:60 <W1A8VL\5#XZ.G!U<V H8V]N M<W0 5"`F=F%L=64I('L-"B` ("`J*"LK*'1H:7,M/G1O<"DI(#T =F%L=64[ M8VL\5#XZ.F]P97)A=&]R/#PH;W-T<F5A;2`F;W,L(&-O;G-T('-T86-K/%0^ M<V4 .R!P='(M+2D >PT*("` ("` ("!O<R`\/"`J<'1R(#P A("` ?0T*("` (')E='5R;B!O<SL-"GT-"B-E;F1I9 T* ` end begin 644 stackDemo.cpp M;'5D92`\:6]S=')E86TN:#X-"B\O($=E="!M>2!S=&%C:R!C;&%S<RX-"B-I M8VL\:6YT/B!F:7)S=#L-"B` ("!S=&%C:SQI;G0^('-E8V]N9" Q*3L-" T* M96-O;F0 /2!F:7)S=#L-" T*("` (&9I<G-T+G!O<" I.PT*("` (&9I<G-T M+G!U<V H,3<I.PT*("` (&-O=70 /#P (F9I<G-T.EQN(CL-"B` ("!C;W5T M(#P\(&9I<G-T.PT*("` (&-O=70 /#P (G-E8V]N9#I ` end
Sep 28 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.
Sep 30 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 begin 644 problem.h M+R\ 26UP;&5M96YT(&$ 9V5N97)I8R!S=&%C:R!C;&%S<R!U<VEN9R!T96UP M('L-" T*("` ('!U8FQI8SH-" T*("` (&9R:65N9"!O<W1R96%M("8 ;W!E M96%M("8 <W1A8VL\5#XZ.F]P97)A=&]R/#PH;W-T<F5A;2`F;W,L(&-O;G-T M<B`^/2!S+F)A<V4 .R!P='(M+2D >PT*("` ("` ("!O<R`\/"`J<'1R(#P\ ` end begin 644 problem.cpp M;'5D92`\:6]S=')E86TN:#X-"B\O($=E="!M>2!S=&%C:R!C;&%S<RX-"B\O M("-I;F-L=61E(")S=&%C:RYH( T*(VEN8VQU9&4 (G!R;V)L96TN:"(-" T* F8V]U="`\/"!F:7)S=#L- ` end
Oct 01 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.
Oct 01 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
Oct 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>.
Oct 03 2006