[Dev-C++] help me check the implemention of Set with template
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Edward H. <hyl...@gm...> - 2007-08-26 08:22:34
|
hi, i'm edward.
i finish a program to implement the set with template. but when i
compling the program, there are some wrong:
[edward@localhost chapter8]$ make -f tset.mk
g++ -c -o tset.o tset.cpp
g++ -o tset tset.o mset.o
tset.o(.text+0x1e): In function `main':
: undefined reference to `Set<int>::Set[in-charge]()'
tset.o(.text+0x41): In function `main':
: undefined reference to `Set<int>::insert(int const&)'
tset.o(.text+0x64): In function `main':
: undefined reference to `Set<int>::insert(int const&)'
tset.o(.text+0x87): In function `main':
: undefined reference to `Set<int>::insert(int const&)'
tset.o(.text+0xd2): In function `main':
: undefined reference to `Set<int>::remove(int const&)'
tset.o(.text+0x12d): In function `main':
: undefined reference to `Set<int>::contains(int const&) const'
tset.o(.gnu.linkonce.t._ZlsIiERSoS0_RK3SetIT_E+0x10): In function
`std::basic_ostream<char, std::char_traits<char> >& operator<<
<int>(std::basic_ostream<char, std::char_traits<char> >&, Set<int>
const&)':
: undefined reference to `Set<int>::print(std::basic_ostream<char,
std::char_traits<char> >&) const'
collect2: ld returned 1 exit status
make: *** [tset] Error 1
my complier is g++3.2
the source code are below
// tset.cpp
#include<iostream>
#include "mset.h"
//using namespace std;
using std::cout;
using std::endl;
int main(){
Set<int> s;
s.insert(77);
s.insert(33);
s.insert(500);
cout << s << endl;
s.remove(77);
cout << s << endl;
cout <<"s "<<(s.contains(77)?"does":"does not")<<" contains 77" << endl;
return 0;
}
// set.h: Set class template
#ifndef MSET_H
#define MSET_H
#include <iostream>
//#include <algorithm>
#include <stddef.h>
template<class T>
class Set{
public:
Set();
bool contains(const T&) const;
void insert(const T&);
void remove(const T&);
void print(std::ostream &)const;
private:
enum{LIMIT = 64};
T elems[LIMIT];
size_t nelems;
};
template<class T>
std::ostream& operator<<(std::ostream& os, const Set<T>& s){
s.print(os);
return os;
}
#endif // SET_H
//: set.cpp: Implementation for Set class template
//#include "set.h"
#include <iostream>
#include <algorithm>
#include <stddef.h>
#include "mset.h"
using std::cout;
using std::endl;
using std::ostream;
template<class T>
Set<T>::Set(){
nelems = 0;
}
template<class T>
bool Set<T>::contains(const T& x) const{
const T* eof = elems + nelems;
return std::find(elems, eof, x) != eof;
}
template<class T>
void Set<T>::insert(const T& x){
if (nelems < LIMIT && !contains(x))
elems[nelems++] = x;
}
template<class T>
void Set<T>::remove(const T& x){
T* eof = elems + nelems;
if (std::remove(elems, eof, x) != eof)
--nelems;
}
template<class T>
void Set<T>::print(std::ostream& os) const
{
os << '{';
for (int i = 0; i < nelems; ++i){
if (i > 0)
os << ',';
os << elems[i];
}
os << '}';
}
thanks for you all!
|