Hey guys i'm very very new to cplus plus and i am creating this data structure for
and array based list class. Im also creating a derived class called unorderedlistType. Unfortunately i have an error in my class unorderedlistType as it cannot detect the protected variables in my base class. Why is that so?
The errors i got is :
In member function 'void unorderredlist<T>::insertEnd(const T&)':
'length' was not declared in this scope
'maxSize' was not declared in this scope
'list' was not declared in this scope
When deriving from a templated class and the derived class is also templated it cannot directly access the members of the base class. The scope is required. So in your case:
1 2 3 4 5 6 7 8 9 10 11 12
template <class T>
void unorderredlist<T>::insertEnd(const T& insertItem){
if (listType<T>::length>=listType<T>::maxSize)
{
cout<< "Cannot insert in a full list. "<< endl;}
else{
listType<T>::list[listType<T>::length]=insertItem;
listType<T>::length++;
}
}
Thanks so much. It now works.
But one more thing why is it that i can't declare the unorderredlist to be size of whatever i want. like this code below:
#include "List Definition.h"
#include <iostream>
using namespace std;
int main(){
unorderredlist<string> stringList(50);
}
error:
(.rdata$_ZTV8listTypeISsE[_ZTV8listTypeISsE]+0x10): undefined reference to `listType<std::string>::insertAt(std::string const&, int)'
below is my updated insertAt function code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
template<class T>
void unorderredlist<T>::insertAt(const T& item, int location)
{
if(location < 0 || location >= unorderredlist<T>::maxSize)
cout<<"The index is out of range."<<endl;
elseif(unorderredlist<T>::length >= unorderredlist<T>::maxSize)
cout<<"The list is full."<<endl;
else
{
for(int i = unorderredlist<T>::length; i > location; i--)
unorderredlist<T>::list[i] = unorderredlist<T>::list[i - 1];
unorderredlist<T>::list[location] = item;
unorderredlist<T>::length++;
}
}
The compiler needs to see the entire definition of an templated function when it is used. I.e. Within a .cpp (which is not include) the templated function is ignored and not seen when use elsewhere. Hence you need to put the entire definition into the header (or other file) which is #include'd