Re: [Dev-C++] help me check the implemention of Set with template
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-08-26 08:54:44
|
Dod you notice anything strange with the errors you got?
The template functions that contains the code in the mset.h file does not
generate any warnings. If you spend some time thingking about it, you
might realize why. Hint: If you make use of inline functions, should the
full implementation of the inline function be visible to the compiler at
the place where you call it?
A template function may be instantiated a number of times, using a lot of
different template parameters. Who is responsible to generate the actual
code for each of these used template data types or limits?
/pwm
On Sun, 26 Aug 2007, Edward Hyl wrote:
> 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!
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|