class template methods
-
I am inheriting from a template class in order to store strings within a list type of structure, CGenericList. I would like to call an instance of this class so that I can use its methods within the implementation of the base class. CSentenceList.h class CSentenceList : CGenericList { public: .... CGenericList Sentences(); //linked list of sentences }; CSentenceList.cpp CSentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted) : CGenericList () {//initialize} ... void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH]) { char strLine[MAX_SENTENCE_LENGTH + 1]; fstream fileToRead(strFileToRead,ios::in); while (!fileToRead.eof() && Size < MAX_LINES) // Read line of text from file and update Sentences.strSentence. { fileToRead.getline(strLine, MAX_SENTENCE_LENGTH); Sentences.Insert(&strLine); // Error 2228: SentenceList.cpp(34): error C2228: left of '.Insert' must have class/struct/union type Size++; } fileToRead.close(); } How can I use methods of Sentences? Is it possible to implement it in another way? Jon
-
I am inheriting from a template class in order to store strings within a list type of structure, CGenericList. I would like to call an instance of this class so that I can use its methods within the implementation of the base class. CSentenceList.h class CSentenceList : CGenericList { public: .... CGenericList Sentences(); //linked list of sentences }; CSentenceList.cpp CSentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted) : CGenericList () {//initialize} ... void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH]) { char strLine[MAX_SENTENCE_LENGTH + 1]; fstream fileToRead(strFileToRead,ios::in); while (!fileToRead.eof() && Size < MAX_LINES) // Read line of text from file and update Sentences.strSentence. { fileToRead.getline(strLine, MAX_SENTENCE_LENGTH); Sentences.Insert(&strLine); // Error 2228: SentenceList.cpp(34): error C2228: left of '.Insert' must have class/struct/union type Size++; } fileToRead.close(); } How can I use methods of Sentences? Is it possible to implement it in another way? Jon
-
I am inheriting from a template class in order to store strings within a list type of structure, CGenericList. I would like to call an instance of this class so that I can use its methods within the implementation of the base class. CSentenceList.h class CSentenceList : CGenericList { public: .... CGenericList Sentences(); //linked list of sentences }; CSentenceList.cpp CSentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted) : CGenericList () {//initialize} ... void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH]) { char strLine[MAX_SENTENCE_LENGTH + 1]; fstream fileToRead(strFileToRead,ios::in); while (!fileToRead.eof() && Size < MAX_LINES) // Read line of text from file and update Sentences.strSentence. { fileToRead.getline(strLine, MAX_SENTENCE_LENGTH); Sentences.Insert(&strLine); // Error 2228: SentenceList.cpp(34): error C2228: left of '.Insert' must have class/struct/union type Size++; } fileToRead.close(); } How can I use methods of Sentences? Is it possible to implement it in another way? Jon
Just out of curiousity, is there some reason you are not using the
string
andlist
(orvector
) classes?If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
typically derived class is whatever is base class. in this case derived class is defined as a base class and can access base class functions template base { public: b(){} }; template derived : base { public: d{} { base::b() }; Kuphryn
-
Just out of curiousity, is there some reason you are not using the
string
andlist
(orvector
) classes?If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Just out of curiousity, is there some reason you are not using the
string
andlist
(orvector
) classes?If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
This is an academic assignment and the tutor discouraged me from using strings, because I can understand arrays of chars more easily I guess. I don't know what vectors are to be honest.
Jon
jon_80 wrote:
This is an academic assignment and the tutor discouraged me from using strings, because I can understand arrays of chars more easily I guess.
Most likely, he (or she) wanted you to understand what actually goes on under the hood of the
string
class (and those like it). This isn't a bad thing, but you should remember that once you understand what it does, there is no need to reinvent the wheel, and doing so is actually bad practice. edit: Is this code for an assignment? If so, what are the restrictions on the assignment?jon_80 wrote:
I don't know what vectors are to be honest.
A
vector
is nothing more than an array. Thevector
template class wraps an array so that you can dynamically add/remove elements in it without worrying about the details of memory allocation/deallocation for the array yourself. -- modified at 13:41 Thursday 10th August, 2006If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Btw, any idea on how I can resolve it, please? I'm sorry but as you know I'm quite new to c++... no wonder everyone says it's 'techy' tho :) Jon
Without seeing what else you have defined in your CSentenceList class, I can't even tell you if you are using a decent design.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Without seeing what else you have defined in your CSentenceList class, I can't even tell you if you are using a decent design.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
CSentenceList.h --------------------------------------------------- class CSentenceList : CGenericList { public: //Methods CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH], bool bSorted = false); virtual ~CSentenceList(void); void sort(); unsigned int search(const char *strSearch); CGenericList Sentences(); //linked list of sentences //Properties unsigned int Size; private: unsigned int searchInSentence(const char* sentence, const char* strSearch); void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH]); }; CSentenceList.cpp (extract) --------------------------- SentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted) : CGenericList () ... void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH]) { char strLine[MAX_SENTENCE_LENGTH + 1]; fstream fileToRead(strFileToRead,ios::in); while (!fileToRead.eof() && Size < MAX_LINES) // Read line of text from file and update Sentences.strSentence. { fileToRead.getline(strLine, MAX_SENTENCE_LENGTH); Sentences.Insert(&strLine); //error C2228: left of '.Insert' must have class/struct/union type Size++; } fileToRead.close(); } Jon