I'm having trouble with a school project. This project runs on commands inputted though the terminal. The problem I'm having is that when I type in the commands: "+a +b +c +d" (insert a, b, c, d) the program breaks at inserting b... so somewhere in the insert command it is screwed up. I'm not sure what though, and if I could be pointed in the right direction! Please edit mycode and post it as last resort... if I could be pointed in the right direction first...

Here is the code for insert:

template <class LE>
void List<LE>::insert ( const LE &newElement )	// Insert after cursor
{
	if (maxSize != size)
		{
			if (size != 0)
			{
				for(int i = (size); i <= (cursor + 1); i--)   
					element[i] = element[(i-1)];
				element[cursor] = newElement;
				cursor++;
			}
			else
			{
				element[0] = newElement;
				cursor = 0;
			}
			size++;
		}
}

I have a feeling it is some kind of logic error, but I'm not sure... I've worked it out on paper, and it seems right.

Here are the individual files:

LISTARR.cpp
LISTARR.h
SHOW4.cpp
TEST4.cpp

If you want to see the whole project in a zip file click here


Thank you!

Avaviel

Look again at this "make room" loop (why parentheses? ):

for (int i = size; i <= cursor + 1; i--)
    element[i] = element[i-1];

Take a paper and a pencil... size = 1, cursor = 0:

1st loop... i = 1; 1 <= 0+1 ... element[1] = element[0] ... OK
2nd loop... i = 0; 0 < 1 ... ???? element[0] = element[-1] ???!!!
and so on...

Thank you for helping!

Well, the code has gone though alot... and now I'm getting an error that has to do with the "template <class LE>" headers... I don't have a clue, truthfully. Tomorrow I'll be seeing my professor in the morning, but if you guys have a clue, any help would be appreciated (but as I said before, not spoon fed!)

Here is the errors:

1>LISTARR.cpp
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2143: syntax error : missing ';' before '<'
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2988: unrecognizable template declaration/definition
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2059: syntax error : '<'
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(29) : error C2588: '::~List' : illegal global destructor
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(29) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>SHOW4.CPP
1>TEST4.CPP
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2143: syntax error : missing ';' before '<'
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2988: unrecognizable template declaration/definition
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(10) : error C2059: syntax error : '<'
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(29) : error C2588: '::~List' : illegal global destructor
1>u:\my documents\visual studio 2005\projects\lab4is\lab4is\listarr.cpp(29) : fatal error C1903: unable to recover from previous error(s); stopping compilation

And here are some code snippits from that area if code:

template <class LE>
List<LE>::List ( int maxNumber = defMaxListSize )
{
	// Set the max size of the array.
	maxSize = maxNumber;
	// Allocate memory for array to hold data.
	element = new LE[maxSize];
	// Set the default size of the array.
	size = 0;
}

template <class LE>
List<LE>::~List ()
{
	// Delete the memory allocated for the array.
	delete [] element;
}

The code LOOKS right... but I've tried it on multiple PCs with the same errors...

Here is LISTARR.cpp, and the rest are the same.

No List class template definition in these constructor and destructor scope.
You must collect ALL List template stuff in a single .h file. Include it in all .cpp modules where List tempate used.

It's impossible now to separate template definition and member templates, no template separate compilation...

Note: [Ark -- I know you know this] but I think it is worth pointing out.

If you think that you HAVE to put the template stuff in a single .h file this is wrong. It is ok and works but really really quickly leads to serious slow down on the compile.

You can also put the code for list (or any other template) in a .cpp file and include only the header in the .h. However, you then (unless you have a nice compile) have to explicitly declare your instances e.g.

template class list<double>; 
template class list<int>;

at the end of you .cpp file.

The main problem with this is that if you want a list<myClass> you have to include the header for myClass in list.cpp. There are ways round that using a third include file etc. but that normally isn't worth the pay back.

None of this matters for small project, but just look at what happens if you change a basic list in Qt3 you spend forever (+1hour) compiling since it is in SO many places. [the simplified a lot in Qt4]

This article has been dead for over six months. Start a new discussion instead.