This one actually tells me visual studio has a problem and needs to close this code. I'm sure it's in the insert/write section, but I'm not sure what I did wrong. I'll post the rather simple cpp below it.

#include <iostream>
using namespace std;

template <class T>
class list
{
public:
	static const int list::size=5;
	list();
	list(int);
	void insert(T);
	void remove(T);
	bool empty();
	void print();
	~list();

private:
	
	T *thelist;
	int numinlist;
	int newsize;

};

template <class T> list<T>::list()		//default
{
	T *thelist = new T[list::size];
	numinlist=0;
	newsize=5;
}

template <class T> list<T>::list(int in_size)		
{
	T *thelist = new T[in_size];
	numinlist=0;
	newsize=in_size;
}

template <class T> void list<T>::insert(T write)
{
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;
		numinlist++;
		cout << "Numinlist is now" << "  " << numinlist << endl;
	}
	else
	{
		newsize++;
		T *temp = new T[newsize];
		for (int i = 0; i < newsize; i++)
		{
				temp[i] = thelist[i];
				delete [] thelist;
		}
		T *thelist = new T[newsize];			
			for (int i = 0; i < newsize; i++)
			{
			thelist[i]= temp[i];
			delete [] temp;
			}
	
		thelist[numinlist] = write;
		numinlist++;
		cout << "Numinlist is now" << "  " << numinlist << endl;	
	}
}


template <class T> void list<T>::remove(T)	//remove function
{
	if(numinlist > 0)
	{
		numinlist--;
		cout << "Numinlist is now" << "  " << numinlist << endl;
	}
	else
	{
		cout << "List is empty" << endl;
		exit(1);
	}
}

template <class T> bool list<T>::empty()	//detect empty function
{
	if(numinlist == 0)
	{
		cout << "List is empty" << endl;
		return true;
	}
	else
	{
		cout << "List is not empty" << endl;
		return false;
	}
}

template <class T> void list<T>::print()	//print function
{
	cout << thelist <<endl;
}

template <class T> list<T>::~list()
{
}

Here is the cpp file

#include "templab.h"

typedef list<int> mylist;
	
int main()
{
	mylist mylist;
	mylist.insert(5);
	mylist.insert(4);
	mylist.insert(3);
	mylist.insert(2);
	mylist.insert(1);
	mylist.insert(6);
	mylist.remove(6);
	mylist.remove(2);
	mylist.remove(3);
	mylist.print();
	return 0;
}

Changed your constructor to initialize the size in this way--

template <class T> list<T>::list() : size(5)		//default
{
	T *thelist = new T[list::size];
	numinlist=0;
	newsize=5;
}

also changed your public static const variable to a private one--

private:
	int size;
	T *thelist;
	int numinlist;
	int newsize;

Then I made a change where you were doing constant damage to the heap, here--

newsize++;
		T *temp = new T[newsize];
		for (int i = 0; i < newsize; i++)
		{
				temp[i] = thelist[i];
                            //delete[] thelist //prior to edit
		}
		     delete[] thelist; //after edit

where you kept deleting the same address over and over in your for loop.

Same problem occurs here--

T *thelist = new T[newsize];
        		
		for (int i = 0; i < newsize; i++)
		{
                     thelist[i]= temp[i];
		}
		   delete [] temp; //after edit

Also, you may want to place the incremented newsize after the copying of the values into the temp pointer, don't do it right off the bat--

T *temp = new T[newsize];
		for (int i = 0; i < newsize; i++)
		{
				temp[i] = thelist[i];
		}
		     delete[] thelist;
		
		newsize++;
		T *thelist = new T[newsize];
        		
		for (int i = 0; i < newsize; i++)
		{
           thelist[i]= temp[i];
		}
		   delete [] temp;

Wow, I knew I'd done something wrong, but I didn't know it was that much...I do see the deletion of the same address over and over again....now

I'll try that out Alex, thanks

Okay, now that all that is in the code, the whole thing dies when I try to make the first input.

Do I need to change how I do the insert?

#include <iostream>
using namespace std;

template <class T>
class list
{
public:
	
	list();
	list(int);
	void insert(T);
	void remove();
	bool empty();
	void print();
	~list();

private:
	int size;
	T *thelist;
	int numinlist;
	int newsize;

};

template <class T> list<T>::list() : size(5), newsize(size)		//default
{
	T *thelist = new T[list<T>::size];
	numinlist=0;
}

template <class T> list<T>::list(int in_size) : size(in_size), newsize(size)		
{
	T *thelist = new T[in_size];
	numinlist=0;
}

template <class T> void list<T>::insert(T write)
{
    
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;
		
		numinlist++;
		cout << "Numinlist is now" << "  " << numinlist << endl;
		cout << thelist[numinlist] << " " << numinlist << endl;
	}
	else
	{
        
		
		T *temp = new T[newsize];
		for (int i = 0; i < newsize; i++)
		{
				temp[i] = thelist[i];
		}
		     delete[] thelist;
		
		newsize++;
		T *thelist = new T[newsize];
        		
		for (int i = 0; i < (newsize - 1); i++)
		{
           thelist[i]= temp[i];
		}
		   delete [] temp;
	
		thelist[newsize - 1] = write;
		numinlist++;
		cout << "Numinlist is now" << "  " << numinlist << endl;	
	}
}


template <class T> void list<T>::remove()	//remove function
{
	if(numinlist > 0)
	{
		numinlist--;
		cout << "Numinlist is now" << "  " << numinlist << endl;
	}
	else
	{
		cout << "List is empty" << endl;
		exit(1);
	}
}

template <class T> bool list<T>::empty()	//detect empty function
{
	if(numinlist == 0)
	{
		cout << "List is empty" << endl;
		return true;
	}
	else
	{
		cout << "List is not empty" << endl;
		return false;
	}
}

template <class T> void list<T>::print()	//print function
{
    for(int i = 0; i < newsize; i++)     
	   cout << thelist[i] << " " << flush;
}

template <class T> list<T>::~list()
{
}

//#include "templab.h"

typedef list<int> mylist;
	
int main()
{
	mylist mylist;
	mylist.insert(5);
	mylist.insert(4);
	mylist.insert(3);
	mylist.insert(2);
	mylist.insert(1);
	mylist.insert(6);
	mylist.remove();
	mylist.remove();
	mylist.remove();
	mylist.print();
    cin.get();
	return 0;
}

I was picking away at certain parts of your class trying to figure out why the very first number wasn't initialized. The code works for me though.

It says it's an unhandled win32 exception in templab.exe

Unhandled exception at 0x00411773 in Template Lab.exe: 0xC0000005: Access violation writing location 0xcccccccc.

and when I look in the locals on the debugger it shows this

- this 0x0012ff48 {size=5 thelist=0xcccccccc numinlist=0 ...} list<int> * const
size 5 int
- thelist 0xcccccccc int *
CXX0030: Error: expression cannot be evaluated
numinlist 0 int
newsize 5 int
write 5 int

It also says this is the next line to execute

template <class T> void list<T>::insert(T write)
{
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;  //(<--this one)

I think its because you assigned memory to a local-scoped T pointer in both constructors and not the actual variable in the private declaration. The local T must have masked the private T.

template <class T> list<T>::list() : size(5), newsize(size)		//default
{
	thelist = new T[list<T>::size]; //used to be T *thelist = new T[5]
	numinlist=0;
}

template <class T> list<T>::list(int in_size) : size(in_size), newsize(size)		
{
	thelist = new T[in_size]; //same here
	numinlist=0;
}

You were right about that, it doesn't blow up on me now but the results are a tad weird

Numinlist is now 1
-842150451 1
Numinlist is now 2
-842150451 2
Numinlist is now 3
-842150451 3
Numinlist is now 4
-842150451 4
Numinlist is now 5
-33686019 5
Numinlist is now 6
Numinlist is now 5
Numinlist is now 4
Numinlist is now 3
-572662307 -572662307 -572662307 -572662307 -572662307 -572662307

The numin list is right, but I'm not sure where those other numbers are coming from yet...lol

could it be this line?

cout << thelist[numinlist] << " " << numinlist << endl;

Yep, that was part of it....oh...try adding another number, that gives an interesting result

Sorry I had that added for debugging purposes =P

Sorry I had that added for debugging purposes =P

it worked, it made me use the debugger...lol

It also doesn't seem to be reporting the numbers entered, at least not correctly, have I managed to dork something up there also? Is the print function not working correctly? It also locks up if I try to insert another number, which makes me think its not increasing size, but more worried about printing out the right numbers first.

I also have to fix my remove block...silly me, it's supposed to be a bool, return true if it finds the object, then remove it and shrink the size. Don't think I'll worry about that though.

any ideas on the display of the numbers inserted in the list from anyone watching would be appreciated, Alex has helped a bunch already but I can't seem to figure out why it isn't displaying correctly

Can you post what you have currently? I'm not getting the same error somehow.

Here is the .h, it should match the one you made changes to, I thought it worked much better your way.

#include <iostream>
using namespace std;

template <class T>
class list
{
	public:
		list();
		list(int);
		void insert(T);
		void remove();
		bool empty();
		void print();
		~list();

	private:	
		int size;	
		T *thelist;	
		int numinlist;	
		int newsize;
};

template <class T> list<T>::list() : size(5),newsize (size)		
{	
	thelist = new T[list::size];	
	numinlist=0;	
}

template <class T> list<T>::list(int in_size): size(in_size), newsize(size)	
{
	thelist = new T[in_size];
	numinlist=0;
}

template <class T> void list<T>::insert(T write)
{
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;
		numinlist++;
		cout << "Number of items in the list is now"  << "  " << numinlist << endl;		
		cout << thelist[numinlist] << " " << numinlist << endl;
	}
	else
	{
		T *temp = new T[newsize];			//make new pointer temp
		for (int i = 0; i < newsize; i++)	//copy list to new pointer		
			{				
				temp[i] = thelist[i];                            
			}
			delete[] thelist;				//delete old pointer the list

		newsize++;							//increment size

		T *thelist = new T[newsize];		//make new pointer the list		
		for (int i = 0; i < (newsize-1); i++)	//copy temp to new pointer
			{
				thelist[i]= temp[i];
			}
			delete [] temp;					//delete temp

		thelist[newsize-1] = write;			
		numinlist++;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;	
	}
}


template <class T> void list<T>::remove()	
{
	if(numinlist > 0)
	{
		numinlist--;
		cout << "Number of items in the list is now"  << "  " << numinlist << endl;
	}
	else
	{
		cout << "List is empty" << endl;
		exit(1);
	}
}

template <class T> bool list<T>::empty()	
{
	if(numinlist == 0)
	{
		cout << "List is empty" << endl;
		return true;
	}
	else
	{
		cout << "List is not empty" << endl;
		return false;
	}
}

template <class T> void list<T>::print()	
{   
	for(int i = 0; i < newsize; i++)     	   
		cout << thelist[i] << " " << endl;
}

template <class T> list<T>::~list()
{
}

Now I changed the cpp a little bit because I had a function written that I wasn't calling (empty), so I put it in a couple times. I'm not so worried about changing the remove, it pulls things off just fine without returning a bool as well or shrinking the stack, but I'd like to get the display right. I'll post those results after the cpp file

#include "templab.h"

typedef list<int> mylist;
	
int main()
{	
	mylist mylist;	
	mylist.insert(5);	
	mylist.insert(6);	
	mylist.insert(4);	
	mylist.insert(2);	
	mylist.insert(1);	
	mylist.insert(3);
	mylist.remove();	
	mylist.remove();
	mylist.remove();
	mylist.empty();
	mylist.remove();
	mylist.empty();
	mylist.print();    
	cin.get();	
	return 0;
}

Here are the results
Number of items in the list is now 1
-842150451 1 <--should this not be displaying 50000 now?
Number of items in the list is now 2
-842150451 2 <--56000......and so on?
Number of items in the list is now 3
-842150451 3
Number of items in the list is now 4
-842150451 4
Number of items in the list is now 5
-33686019 5
Number of items in the list is now 6
Number of items in the list is now 5
Number of items in the list is now 4
Number of items in the list is now 3
List is not empty
Number of items in the list is now 2
List is not empty
-572662307
-572662307
-572662307
-572662307
-572662307
-572662307

I only made a small change - its commented in the code below--

#include <iostream>
using namespace std;

template <class T>
class list
{
	public:
		list();
		list(int);
		void insert(T);
		void remove();
		bool empty();
		void print();
		~list();

	private:	
		int size;	
		T *thelist;	
		int numinlist;	
		int newsize;
};

template <class T> list<T>::list() : size(5),newsize (size)		
{	
	thelist = new T[list::size];	
	numinlist=0;	
}

template <class T> list<T>::list(int in_size): size(in_size), newsize(size)	
{
	thelist = new T[in_size];
	numinlist=0;
}

template <class T> void list<T>::insert(T write)
{
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;
		numinlist++;
		cout << "Number of items in the list is now"  << "  " << numinlist << endl;		
		cout << thelist[numinlist - 1] << " " << (numinlist - 1) << endl;
	}     //^^ above, changed it so it displayed the number that was just entered
	else
	{
		T *temp = new T[newsize];			//make new pointer temp
		for (int i = 0; i < newsize; i++)	//copy list to new pointer		
			{				
				temp[i] = thelist[i];                            
			}
			delete[] thelist;				//delete old pointer the list

		newsize++;							//increment size

		T *thelist = new T[newsize];		//make new pointer the list		
		for (int i = 0; i < (newsize-1); i++)	//copy temp to new pointer
			{
				thelist[i]= temp[i];
			}
			delete [] temp;					//delete temp

		thelist[newsize-1] = write;			
		numinlist++;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;	
	}
}


template <class T> void list<T>::remove()	
{
	if(numinlist > 0)
	{
		numinlist--;
		cout << "Number of items in the list is now"  << "  " << numinlist << endl;
	}
	else
	{
		cout << "List is empty" << endl;
		exit(1);
	}
}

template <class T> bool list<T>::empty()	
{
	if(numinlist == 0)
	{
		cout << "List is empty" << endl;
		return true;
	}
	else
	{
		cout << "List is not empty" << endl;
		return false;
	}
}

template <class T> void list<T>::print()	
{   
	for(int i = 0; i < newsize; i++)     	   
		cout << thelist[i] << " " << endl;
}

template <class T> list<T>::~list()
{
         
}

typedef list<int> mylist;
	
int main()
{	
	mylist mylist;	
	mylist.insert(5);	
	mylist.insert(6);	
	mylist.insert(4);	
	mylist.insert(2);	
	mylist.insert(1);	
	mylist.insert(3);
	mylist.remove();	
	mylist.remove();
	mylist.remove();
	mylist.empty();
	mylist.remove();
	mylist.empty();
	mylist.print();    
	cin.get();	
	return 0;
}

--As for your remove method, you're going to have to do 3 things--

-Copy the contents of your current pointer to a temp one.
-Resize your pointer - this shouldn't be hard since you know how to increase the size
-Decrement the value that lists the amount of values in the pointer
-Copy the contents of the temp pointer into the new pointer

--You said this wasn't the current version right? Can you show me the current that you have now?

this is the current, I was just letting you know that I incorporated your earlier changes, and added a couple of calls to the "empty" function in the cpp file

what is that number at the end? I thought that would be a list of the contents, is it the beginning address instead?

I was actually trying to get it to print out the array, but I'm not doing so hot at that

Something like this for the removal code?

T *temp = new T[newsize];			
		for (int i = 0; i < newsize; i--)		
			{				
			temp[i] = thelist[i];                            
			}
			delete[] thelist;				

		newsize--;

		T *thelist = new T[newsize];			
		for (int i = 0; i < (newsize-1); i--)	
			{
			     thelist[i]= temp[i];
			}
			delete [] temp;								
		numinlist--;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;

Something like this for the removal code?

T *temp = new T[newsize];			
		for (int i = 0; i < newsize; i--)		
			{				
			temp[i] = thelist[i];                            
			}
			delete[] thelist;				

		newsize--;

		T *thelist = new T[newsize]; //error	
		for (int i = 0; i < (newsize-1); i--)	
			{
			     thelist[i]= temp[i];
			}
			delete [] temp;								
		numinlist--;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;

Ok I think I found another error

In your remove you declare thelist as a local variable which masks the real value of thelist.

This seems to work well so far--

#include <iostream>
using namespace std;

template <class T>
class list
{
	public:
		list();
		list(int);
		void insert(T);
		void remove();
		bool empty();
		void print();
		~list();

	private:	
		int size;	
		T *thelist;	
		int numinlist;	
		int newsize;
};

template <class T> list<T>::list() : size(5),newsize (size)		
{	
	thelist = new T[list::size];	
	numinlist=0;	
}

template <class T> list<T>::list(int in_size): size(in_size), newsize(size)	
{
	thelist = new T[in_size];
	numinlist=0;
}

template <class T> void list<T>::insert(T write)
{
	if(numinlist < list::size)
	{
		thelist[numinlist] = write;
		numinlist++;
		cout << "Number of items in the list is now"  << "  " << numinlist << endl;		
		cout << thelist[numinlist - 1] << " " << (numinlist - 1) << endl;
	}
	else
	{
		T *temp = new T[newsize];			//make new pointer temp
		for (int i = 0; i < newsize; i++)	//copy list to new pointer		
			{				
				temp[i] = thelist[i];                            
			}
			delete[] thelist;				//delete old pointer the list

		newsize++;							//increment size

		thelist = new T[newsize];		//make new pointer the list		
		for (int i = 0; i < (newsize-1); i++)	//copy temp to new pointer
			{
				thelist[i]= temp[i];
			}
			delete [] temp;					//delete temp

		thelist[newsize-1] = write;			
		numinlist++;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;	
	}
}


template <class T> void list<T>::remove()	
{
         
	if(numinlist > 0)
	{
		T *temp = new T[newsize];			
		for (int i = 0; i < newsize; i++)		
			{				
			     temp[i] = thelist[i];                            
			}
			delete[] thelist;				

		newsize--;

		thelist = new T[newsize];			
		for (int i = 0; i < (newsize); i++)	
			{
			     thelist[i]= temp[i];
			}
			delete [] temp;								
	    numinlist--;
		cout << "Number of items in the list is now" << "  " << numinlist << endl;	
	}
	else
	{
		cout << "List is empty" << endl;
		exit(1);
	}
}

template <class T> bool list<T>::empty()	
{
	if(numinlist == 0)
	{
		cout << "List is empty" << endl;
		return true;
	}
	else
	{
		cout << "List is not empty" << endl;
		return false;
	}
}

template <class T> void list<T>::print()	
{   
	for(int i = 0; i < numinlist; i++)     	   
		cout << thelist[i] << " " << endl;
}

template <class T> list<T>::~list()
{
         
}

typedef list<int> mylist;
	
int main()
{	
	mylist mylist;	
	mylist.insert(5);	
	mylist.insert(6);	
	mylist.insert(4);	
	mylist.insert(2);	
	mylist.insert(1);	
	mylist.insert(3);	
	mylist.remove();
	mylist.remove();
	mylist.remove();
	mylist.remove();
	mylist.remove();
	mylist.empty();
	mylist.remove();
	mylist.insert(100);
	mylist.insert(53);
	mylist.empty();
	mylist.print();   
	cin.get();	
	return 0;
}

I didn't see much of a change, but it works

Thank you yet again Alex, I've been tearing my hair out today trying to get the seemingly simplest things to work right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.