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. Extending a class that uses a template

Extending a class that uses a template

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
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.
  • P Offline
    P Offline
    Patrick G
    wrote on last edited by
    #1

    I'm needing to add functionality to a class that uses a template. In other words, there is a template class in a library that I'm using, I need to use it (there is no escaping it), but I really need to pass an additional parameter to it either via constructor (my preferred solution) or by using a mutator method. The problem is that the compiler refuses to allow me to do two things: 1) add a parameter to the class, and 2) add a parameter to a constructor or add a new method to my class. Is there a 'proper' way to do this?

    L 1 Reply Last reply
    0
    • P Patrick G

      I'm needing to add functionality to a class that uses a template. In other words, there is a template class in a library that I'm using, I need to use it (there is no escaping it), but I really need to pass an additional parameter to it either via constructor (my preferred solution) or by using a mutator method. The problem is that the compiler refuses to allow me to do two things: 1) add a parameter to the class, and 2) add a parameter to a constructor or add a new method to my class. Is there a 'proper' way to do this?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Patrick G wrote:

      the compiler refuses to allow me to do two things

      We can't understand the compiler errors unless you post them along with the relevant code.

      led mike

      P 1 Reply Last reply
      0
      • L led mike

        Patrick G wrote:

        the compiler refuses to allow me to do two things

        We can't understand the compiler errors unless you post them along with the relevant code.

        led mike

        P Offline
        P Offline
        Patrick G
        wrote on last edited by
        #3

        Context: I want to have a pointer to an object as a member of a class that uses a template. All the errors I show below point to the lines of code that I've provided. Example 1: Add a private parameter and change it using a public mutator. Here's the private parameter: CommunicationSystem* comms; Here's the mutator declaration: void setCommSystem(CommunicationSystem* commSystem); Here's the implementation of the mutator:

        void ReactorServiceHandler::setCommSystem(CommunicationSystem* commSystem) {
        this->comms = commSystem;
        }

        Here's the call of the mutator: acceptor.setCommSystem(commSystem); Note it doesn't much matter if I use the "." or the "->", I get identical errors. Here's 9 errors complaining about it:

        Error 1 error C2039: 'setCommSystem' : is not a member of 'Poco::Net::SocketAcceptor' c:\Documents and Settings\patrick\My Documents\VirtualWorld\Cave\src\ReactorServer.cpp 125
        Error 2 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
        Error 3 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
        Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
        Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
        Error 6 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
        Error 7 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
        Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
        Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105

        Switching the parameter from private to public and getting ri

        L D 2 Replies Last reply
        0
        • P Patrick G

          Context: I want to have a pointer to an object as a member of a class that uses a template. All the errors I show below point to the lines of code that I've provided. Example 1: Add a private parameter and change it using a public mutator. Here's the private parameter: CommunicationSystem* comms; Here's the mutator declaration: void setCommSystem(CommunicationSystem* commSystem); Here's the implementation of the mutator:

          void ReactorServiceHandler::setCommSystem(CommunicationSystem* commSystem) {
          this->comms = commSystem;
          }

          Here's the call of the mutator: acceptor.setCommSystem(commSystem); Note it doesn't much matter if I use the "." or the "->", I get identical errors. Here's 9 errors complaining about it:

          Error 1 error C2039: 'setCommSystem' : is not a member of 'Poco::Net::SocketAcceptor' c:\Documents and Settings\patrick\My Documents\VirtualWorld\Cave\src\ReactorServer.cpp 125
          Error 2 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
          Error 3 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
          Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
          Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
          Error 6 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
          Error 7 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
          Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
          Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105

          Switching the parameter from private to public and getting ri

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Wow, Patrick in the first listing of errors #1 and #2 are beginner C++ compiler errors. Also you voted my post as not helpful so I have no motivation to help you. However you need to help yourself first by learning how to program in C++ if you intend to, well, program in C++.

          led mike

          P 1 Reply Last reply
          0
          • L led mike

            Wow, Patrick in the first listing of errors #1 and #2 are beginner C++ compiler errors. Also you voted my post as not helpful so I have no motivation to help you. However you need to help yourself first by learning how to program in C++ if you intend to, well, program in C++.

            led mike

            P Offline
            P Offline
            Patrick G
            wrote on last edited by
            #5

            Led Mike, I'm sorry if you didn't like me voting your response as not useful, but since it wasn't an answer, I'm afraid it really wasn't useful. That being said, I do appreciate any assistance. Also, I appreciate that people do in take the time to read and help other people out with their problems. Now, with all of that being said, in my (very long) experience with C++ compilers I've found that when the compiler gets confused it tends to spit out trash for error messages. You mentioned that the errors are "novice" mistakes (since they are referring to semicolons and such). I guarantee that there are no semicolons missing. I know this because when I take out my function calls (listed above, semicolons and all) all the troubles go away. (I should point out that in example 2 I forgot to post the deceleration of my private member variable, it is in the code though, as in example 1). Also, as in the code shown above, the variables that are claimed by the compiler to not be declared in fact are, but seem to be ignored by the compiler. This is actually why I didn't give any errors in my first post and why I only summarized the problem (I figured a print out of nonsensical compiler errors would be a waste of disk space). All of this led me to believe that when a class uses a template that it can't have additional parameters added to it lightly, although I highly doubt that it's impossible.

            L 1 Reply Last reply
            0
            • P Patrick G

              Context: I want to have a pointer to an object as a member of a class that uses a template. All the errors I show below point to the lines of code that I've provided. Example 1: Add a private parameter and change it using a public mutator. Here's the private parameter: CommunicationSystem* comms; Here's the mutator declaration: void setCommSystem(CommunicationSystem* commSystem); Here's the implementation of the mutator:

              void ReactorServiceHandler::setCommSystem(CommunicationSystem* commSystem) {
              this->comms = commSystem;
              }

              Here's the call of the mutator: acceptor.setCommSystem(commSystem); Note it doesn't much matter if I use the "." or the "->", I get identical errors. Here's 9 errors complaining about it:

              Error 1 error C2039: 'setCommSystem' : is not a member of 'Poco::Net::SocketAcceptor' c:\Documents and Settings\patrick\My Documents\VirtualWorld\Cave\src\ReactorServer.cpp 125
              Error 2 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
              Error 3 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
              Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
              Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
              Error 6 error C2061: syntax error : identifier 'CommunicationSystem' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 94
              Error 7 error C2143: syntax error : missing ';' before '*' c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
              Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105
              Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\patrick\my documents\virtualworld\cave\include\ReactorServiceHandler.h 105

              Switching the parameter from private to public and getting ri

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Patrick G wrote:

              Error 1 error C2039: 'setCommSystem' : is not a member of 'Poco::Net::SocketAcceptor' c:\Documents and Settings\patrick\My Documents\VirtualWorld\Cave\src\ReactorServer.cpp 125

              What does line 125 (and possibly the few preceding it) of ReactorServer.cpp look like?

              "Love people and use things, not love things and use people." - Unknown

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              P 1 Reply Last reply
              0
              • D David Crow

                Patrick G wrote:

                Error 1 error C2039: 'setCommSystem' : is not a member of 'Poco::Net::SocketAcceptor' c:\Documents and Settings\patrick\My Documents\VirtualWorld\Cave\src\ReactorServer.cpp 125

                What does line 125 (and possibly the few preceding it) of ReactorServer.cpp look like?

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                P Offline
                P Offline
                Patrick G
                wrote on last edited by
                #7

                Sorry, I should have posted that with my original code. I assume you're referring to the first example. This was the call to the mutator acceptor.setCommSystem(commSystem);. The previous lines of code are initializations for other classes that are irrelevant to this problem with the exception of the call to the constructor just prior to this. That was SocketAcceptor acceptor(svs, reactor); Here's a little summary of the files so that this all makes more sense. ReactorServer is the class that is trying to declare a ReactorServiceHandler which uses the template. CommunicationSystem* comms (or commSystem, depending on where you read it in the post) is just a pointer to an object that manages these objects (among others) that deal with communications. (It's similar to the Facade design pattern).

                D 1 Reply Last reply
                0
                • P Patrick G

                  Sorry, I should have posted that with my original code. I assume you're referring to the first example. This was the call to the mutator acceptor.setCommSystem(commSystem);. The previous lines of code are initializations for other classes that are irrelevant to this problem with the exception of the call to the constructor just prior to this. That was SocketAcceptor acceptor(svs, reactor); Here's a little summary of the files so that this all makes more sense. ReactorServer is the class that is trying to declare a ReactorServiceHandler which uses the template. CommunicationSystem* comms (or commSystem, depending on where you read it in the post) is just a pointer to an object that manages these objects (among others) that deal with communications. (It's similar to the Facade design pattern).

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Have you considered trying to reproduce this scenario with a much simpler set of classes? As it is, there's way too much code here, both shown and not, to wade through and have any chance of understanding it.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • P Patrick G

                    Led Mike, I'm sorry if you didn't like me voting your response as not useful, but since it wasn't an answer, I'm afraid it really wasn't useful. That being said, I do appreciate any assistance. Also, I appreciate that people do in take the time to read and help other people out with their problems. Now, with all of that being said, in my (very long) experience with C++ compilers I've found that when the compiler gets confused it tends to spit out trash for error messages. You mentioned that the errors are "novice" mistakes (since they are referring to semicolons and such). I guarantee that there are no semicolons missing. I know this because when I take out my function calls (listed above, semicolons and all) all the troubles go away. (I should point out that in example 2 I forgot to post the deceleration of my private member variable, it is in the code though, as in example 1). Also, as in the code shown above, the variables that are claimed by the compiler to not be declared in fact are, but seem to be ignored by the compiler. This is actually why I didn't give any errors in my first post and why I only summarized the problem (I figured a print out of nonsensical compiler errors would be a waste of disk space). All of this led me to believe that when a class uses a template that it can't have additional parameters added to it lightly, although I highly doubt that it's impossible.

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #9

                    Patrick G wrote:

                    but since it wasn't an answer, I'm afraid it really wasn't useful.

                    But David's post was an answer? Whatever dude, good luck.

                    DavidCrow wrote:

                    What does line 125 (and possibly the few preceding it) of ReactorServer.cpp look like?

                    led mike

                    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