On Tuesday 02 June 2009 08:16:35 Alex Luya wrote: > I download source code for book <</Data Structures and Algorithm > Analysis in C++ (Second Edition), /by Mark Allen Weiss>> > from:http://users.cs.fiu.edu/~weiss/dsaa_c++/code/,try to compiler > it,but got many errors,most of them say: > ...... previously declared here > .......: redefinition of ..... > > I think template causes these errors,but how to fix it.
This is not the correct mailing list for such questions! Nevertheless, the reason for your compile errors is a simple one. Just drop the line #include "StackAr.cpp" from your header file. Why are you trying to include the implementation in the header? The other way round is how things work! (And you do have the header include in your implementation - why both directions?) > ----------------------------------------------------------------------- > My configuration: > Ubuntu 9.04 > GCC version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) > Eclipse 3.4 > CDT:.5.0.2 > ------------------------------------------------------------------------- > > Files and error message are following: > > StackAr.h > ----- > #ifndef STACKAR_H > #define STACKAR_H > > #include "../vector.h" > #include "../dsexceptions.h" > > template <class Object> > class Stack > { > public: > explicit Stack( int capacity = 10 ); > bool isEmpty( ) const; > ............. > #include "StackAr.cpp" > #endif > > -------------- > > StackAr.cpp > > #include "StackAr.h" > template <class Object> > Stack<Object>::Stack( int capacity ) : theArray( capacity ) > { > topOfStack = -1; > } > > template <class Object> > bool Stack<Object>::isEmpty( ) const > { > return topOfStack == -1; > } > ........... > > Test.cpp > #include <iostream> > #include "StackAr.h" > using namespace std; > > int main() > { > Stack<int> s; > > for (int i = 0; i < 10; i++) > s.push(i); > > while (!s.isEmpty()) > cout << s.topAndPop() << endl; > return 0; > } > > > --------------------------------------------- > error message: > > **** Build of configuration Debug for project DACPP **** > > make all > Building file: ../src/stack/StackAr.cpp > Invoking: GCC C++ Compiler > g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP > -MF"src/stack/StackAr.d" -MT"src/stack/StackAr.d" > -o"src/stack/StackAr.o" "../src/stack/StackAr.cpp" > ../src/stack/StackAr.cpp:7: erreur: redefinition of > ‘Stack<Object>::Stack(int)’ > ../src/stack/StackAr.cpp:7: erreur: ‘Stack<Object>::Stack(int)’ > previously declared here > ../src/stack/StackAr.cpp:17: erreur: redefinition of ‘bool > Stack<Object>::isEmpty() const’ > ../src/stack/StackAr.cpp:17: erreur: ‘bool Stack<Object>::isEmpty() > const’ previously declared here > ............... -- <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> <> <> <> Tim München, M.Sc. muenc...@physik.uni-wuppertal.de <> <> Bergische Universitaet <> <> FB C - Physik Tel.: +49 (0)202 439-3521 <> <> Gaussstr. 20 Fax : +49 (0)202 439-2811 <> <> 42097 Wuppertal <> <> <> <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>