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.
-----------------------------------------------------------------------
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
...............