From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9191 invoked by alias); 2 Jun 2009 06:34:19 -0000 Received: (qmail 9175 invoked by uid 22791); 2 Jun 2009 06:34:18 -0000 X-SWARE-Spam-Status: No, hits=2.8 required=5.0 tests=BAYES_60,J_CHICKENPOX_23,J_CHICKENPOX_63,J_CHICKENPOX_64 X-Spam-Check-By: sourceware.org Received: from mail.physik.uni-wuppertal.de (HELO mail.physik.uni-wuppertal.de) (132.195.104.215) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 02 Jun 2009 06:34:13 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.physik.uni-wuppertal.de (Postfix) with ESMTP id 40FB06734DF8 for ; Tue, 2 Jun 2009 08:34:42 +0200 (CEST) Received: from mail.physik.uni-wuppertal.de ([127.0.0.1]) by localhost (mail.physik.uni-wuppertal.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 08749-06 for ; Tue, 2 Jun 2009 08:34:38 +0200 (CEST) Received: from endor.local (unknown [194.95.9.101]) by mail.physik.uni-wuppertal.de (Postfix) with ESMTP id EFAC36734DDA for ; Tue, 2 Jun 2009 08:34:37 +0200 (CEST) From: Tim =?utf-8?q?M=C3=BCnchen?= To: gcc@gcc.gnu.org Subject: Re: Link error ....redefinition of...... Date: Tue, 02 Jun 2009 06:34:00 -0000 User-Agent: KMail/1.9.10 References: <4A24C3C3.4030208@yeah.net> In-Reply-To: <4A24C3C3.4030208@yeah.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200906020834.04785.muenchen@physik.uni-wuppertal.de> X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-06/txt/msg00019.txt.bz2 On Tuesday 02 June 2009 08:16:35 Alex Luya wrote: > I download source code for book < 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= =20 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=20 header? The other way round is how things work! (And you do have the header= =20 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 Stack > { > public: > explicit Stack( int capacity =3D 10 ); > bool isEmpty( ) const; > ............. > #include "StackAr.cpp" > #endif > > -------------- > > StackAr.cpp > > #include "StackAr.h" > template > Stack::Stack( int capacity ) : theArray( capacity ) > { > topOfStack =3D -1; > } > > template > bool Stack::isEmpty( ) const > { > return topOfStack =3D=3D -1; > } > ........... > > Test.cpp > #include > #include "StackAr.h" > using namespace std; > > int main() > { > Stack s; > > for (int i =3D 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=3D0 -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 > =E2=80=98Stack::Stack(int)=E2=80=99 > ../src/stack/StackAr.cpp:7: erreur: =E2=80=98Stack::Stack(int)=E2= =80=99 > previously declared here > ../src/stack/StackAr.cpp:17: erreur: redefinition of =E2=80=98bool > Stack::isEmpty() const=E2=80=99 > ../src/stack/StackAr.cpp:17: erreur: =E2=80=98bool Stack::isEmpty= () > const=E2=80=99 previously declared here > ............... --=20 <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> <> <> <> Tim M=C3=BCnchen, M.Sc. muenchen@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 <> <> <> <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>