Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. class template methods

class template methods

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++iosdata-structureshelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    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

    V Z 2 Replies Last reply
    0
    • J jon 80

      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

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • J jon 80

        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

        Z Offline
        Z Offline
        Zac Howland
        wrote on last edited by
        #3

        Just out of curiousity, is there some reason you are not using the string and list (or vector) 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

        J 2 Replies Last reply
        0
        • V valikac

          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

          J Offline
          J Offline
          jon 80
          wrote on last edited by
          #4

          I think that's what I'm doing, but I get compiler error C2228. Jon

          1 Reply Last reply
          0
          • Z Zac Howland

            Just out of curiousity, is there some reason you are not using the string and list (or vector) 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

            J Offline
            J Offline
            jon 80
            wrote on last edited by
            #5

            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

            Z 1 Reply Last reply
            0
            • Z Zac Howland

              Just out of curiousity, is there some reason you are not using the string and list (or vector) 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

              J Offline
              J Offline
              jon 80
              wrote on last edited by
              #6

              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

              Z 1 Reply Last reply
              0
              • J jon 80

                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

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                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. The vector 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, 2006

                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

                1 Reply Last reply
                0
                • J jon 80

                  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

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #8

                  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

                  J 1 Reply Last reply
                  0
                  • Z Zac Howland

                    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

                    J Offline
                    J Offline
                    jon 80
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups