So I'm having another problem, this time with virtual functions and templates. I have this base class header file, truncated for only the essential details:
template<class elemType>
void unorderedArrayListType<elemType>::insertAt(int location,
elemType& insertItem)
{
if (location < 0 || location >= this->maxSize)
cout << "Position of the item to be inserted is out of range." << endl;
elseif (this->length >= this->maxSize)
cout << "List is full." << endl;
else {
for (int i = this->length; i > location; --i)
this->list[i] = this->list[i - 1]; /move elements down
this->list[location] = insertItem; //insert at specified position
this->length++;
}
}
In the interest of brevity, I'll limit it to this code.
When I try to declare, let's say unorderedArrayListType<int> intList in main, I get an error saying "cannot declare variable intList to be of type unorderedArrayListType<int> because the following functions are abstract," then it lists three of the five functions I listed above, insertAt among them (the other two were insertEnd and ReplaceAt). I didn't have this problem with the other two functions.
Oh, and for some reason unorderedArrayListType wasn't recognizing/accessing private members of arrayListType in spite of the public inheritance, hence the abundant this-> placements.
So! Can anybody give me a hand here? I'll of course be glad to supply more code should the need arise.
It is because in the arrayListType base class, those function have const elemType& function parameters. virtualvoid insertAt(int location, const elemType& insertItem) = 0;
In unorderedArrayListType class, you have forgotton the const keyword void insertAt(int location, /*const keyword missing*/elemType& insertItem);
So this is a function mismatch so no actual virtual function overrideing has taken place.
Hey thanks. Don't know how many times I've missed something like that. I made the changes and found some new errors. Some pesky Linker errors. I've been able to wiggle my way out of some linker errors, but am stumped with these.
"[Linker error] undefined reference to unorderedListType<int>::insertAt(int, int const&)"
As well as similar error statements for every function in unorderedArrayListType... and a couple from the base class. And all I wanted to do was declare unorderedArrayListType<int> intList in main. My syntax with the headers seems to be fine (#define, #ifndef, #endif, #include "unorderedarraytype.h, and so on). Anybody know how to solve this?
Have you split the template classes into seperate h files and cpp files - for example unorderedArrayListType.h and unorderedArrayListType.cpp ??
If you have, then you best change it to have the class declaration and definitions in the header file only (for example - put all the class stuff for unorderedArrayListType class into a single unorderedArrayListType.h file - and forget about the cpp file)
Oh, and for some reason unorderedArrayListType wasn't recognizing/accessing private members of arrayListType in spite of the public inheritance, hence the abundant this-> placements.
That's because you declare them private. If you want to access in the derived class, you need to declare them protected (I think it shouldn't work with the this)
I do usually split up classes into separate h and cpp files. Putting them into one more or less solved all the linker errors. Don't know why this is.
Also, the original problem did have them as protected instead of private. I think I just changed them to private to experiment, but forgot to change them back.
I do usually split up classes into separate h and cpp files. Putting them into one more or less solved all the linker errors. Don't know why this is.
The splitting up of classes into seperate h and cpp files works for for normal C++ classes - it does not work so straight- forwardly for template classes/functions.