Templates

This is a discussion on Templates within the C++ Programming forums, part of the General Programming Boards category; Having trouble running the blue line in this code: Code: #include <iostream> #include "ArrayList.h" using namespace std; int main() { ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    Templates

    Having trouble running the blue line in this code:
    Code:
    #include <iostream>
    #include "ArrayList.h"
    
    using namespace std;
    int main()
    {
    	ArrayList<int> al(10);
              al.add(5);
    	return 0;
    }
    Heres arraylist.h

    Code:
    //ArrayList.h
    #include <vector>
    using namespace std;
    
    template <class T>
    class ArrayList
    {
    public:
    	ArrayList(const int sz = 10);
    	T get(const int &index);
    	void add(const T &in);
    	T remove(const int &index);
    	int size();
    private:
    	int avail;
    	vector<T> arr;
    
    };
    
    
    template <class T>
    ArrayList<T>::ArrayList(const int sz = 10)
    {
    	avail = 0;
    	vector<T> temp(sz);
    	arr = temp;
    }
    
    
    template <class T>
    void ArrayList<T>::add(const T &in)
    {
    	arr[avail] = T;
    	++avail;
    }
    
    template <class T>
    T ArrayList<T>::get(const int &index)
    {
    	return arr[index];
    }
    template <class T>
    int ArrayList<T>::size()
    {
    	return avail;
    }
    
    
    
    template <class T>
    T ArrayList<T>::remove(const int &index)
    {
    	T temp = arr[index];
    	for(int i=index;i<avail;++i)
    	{
    		arr[index] = index+1;
    	}
    	--avail;
    	return temp;
    }
    any idea what i am doing wrong?
    Last edited by rodrigorules; 11-22-2009 at 09:05 PM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Quote Originally Posted by rodrigorules View Post
    Having trouble running the blue line in this code:
    Code:
    template <class T>
    void ArrayList<T>::add(const T &in)
    {
    	arr[avail] = T;
    	++avail;
    }
    That looks like it should be
    Code:
    template <class T>
    void ArrayList<T>::add(const T &in)
    {
    	arr[avail] = in;
    	++avail;
    }

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    ah ops...

    i must have stared at the code for almost half hour

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Heh, I know exactly how you feel.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Actually, it should be:
    Code:
    template <class T>
    void ArrayList<T>::add(const T &in)
    {
    	arr.push_back( in );
    	++avail;
    }
    otherwise you'll be adding elements past the end of your vector and corrupting your memory (or crashing if you're lucky)... You need to make similar changes to the rest of your functions as well.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    What is the point of this weird wrapper around vector<T>?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21