Thread: Problem with template usage

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Problem with template usage

    I'm trying to make a little vector type thing but I get 5 errors when trying to compile the following code, I've never really used templates before ;_;

    Here's the code:
    Code:
    #include <iostream.h>
    #include <stdarg.h>
    
    template <class hndl>
    
    class myvector {
    public:
    	hndl *vectype;
    
    	myvector(int basesize = 1);
    	~myvector();
    
    	hndl getaverage (void);
    
    	void resize (int newsize);
    	void set_v (int start, int finish, ...);
    	void output (ostream & location);
    	void output (int start, int finish, ostream & location);
    
    	int getsize (void);
    	int getmid (void);
    	int getlow (int start);
    private:
    	int size;
    };
    
    hndl myvector::myvector(int basesize) 
    {
    	vectype = new   hndl [basesize];
    	size    = basesize;
    }
    
    hndl myvector::~myvector()
    {
    	delete [] vectype;
    	vectype = NULL;
    }
    
    void myvector::resize(int newsize)
    {
    	delete [] vectype;
    	vectype = new hndl [newsize];
    	size    = newsize;
    }
    
    hndl myvector::getaverage(void)
    {
    	auto int i;
    	hndl t;
    	for (i = 0; i < size; ++i) 
    		t += vectype[i];
    	t /= size;
    	return (t);	
    }
    
    int myvector::getlow(int start)
    {
    	int lowspot = start;
    	for (auto int c = start; c < size; ++c) {
    		if (vectype[c] < vectype[lowspot])
    		lowspot = c;
    	}
    	return lowspot;
    }
    
    int myvector::getmid(void)
    {
    	return int(size/2);
    }
    
    void myvector:: output(int start, int finish, ostream &location)
    {
    	for (int i = start; i < finish; ++i)
    		cout << vectype[i] << ", ";
    }
    
    void myvector:: output(ostream & location)
    {
    	for (int i = 0; i < size; ++i)
    		cout << vectype[i] << ", ";
    }
    
    void myvector::set_v(int start, int finish, ...)
    {
    	va_list args;
    	va_start (args, nm);
    	for (int i = st; i < nm; ++i) {
    		vectype[i] = va_arg(args, hndl);
    	}
    	va_end (args);
    }
    And taking the "hndl" out before the constructor/deconstructor creates maaaaaaaannnnnny errors... ;_;

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to templatise the member functions.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    One thing to watch out for is MSVC, which doesn't fully support templates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 02:10 AM
  2. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  3. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM