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. pointer to class as arguement in a function

pointer to class as arguement in a function

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

    What is the proper way to define a function that contains a class pointer as an arguement (because I am having very bad compile errors all over the place) Is this the correct way: void MyFunction(char*, char*, CClass*); or void MyFunction(char*, char*, class CClass*); The former gives me a syntax error eg. identifyer CClass The later gives me compile error when writing a differnt function such as void MyFunction2(char*, char*, CClass::CSubClass*); void MyFunction2(char*, char*, class CClass::cSubClass*); Every time I change one way of doing it, I screw the code up somewhere else. Can I get some help on this one? Thanks

    P N 2 Replies Last reply
    0
    • G georgiek50

      What is the proper way to define a function that contains a class pointer as an arguement (because I am having very bad compile errors all over the place) Is this the correct way: void MyFunction(char*, char*, CClass*); or void MyFunction(char*, char*, class CClass*); The former gives me a syntax error eg. identifyer CClass The later gives me compile error when writing a differnt function such as void MyFunction2(char*, char*, CClass::CSubClass*); void MyFunction2(char*, char*, class CClass::cSubClass*); Every time I change one way of doing it, I screw the code up somewhere else. Can I get some help on this one? Thanks

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      void MyFunction(char* c1, char* c2, CClass* theObject);
      void MyFunction2(char* c1, char* c2, CSubClass* theObject);

      Note that if CSubClass extends CClass then you can pass a CSubClass* as the argument to CClass*

      "No matter where you go, there your are." - Buckaroo Banzai

      -pete

      1 Reply Last reply
      0
      • G georgiek50

        What is the proper way to define a function that contains a class pointer as an arguement (because I am having very bad compile errors all over the place) Is this the correct way: void MyFunction(char*, char*, CClass*); or void MyFunction(char*, char*, class CClass*); The former gives me a syntax error eg. identifyer CClass The later gives me compile error when writing a differnt function such as void MyFunction2(char*, char*, CClass::CSubClass*); void MyFunction2(char*, char*, class CClass::cSubClass*); Every time I change one way of doing it, I screw the code up somewhere else. Can I get some help on this one? Thanks

        N Offline
        N Offline
        Nynaeve
        wrote on last edited by
        #3

        class CClass; // OR: #include "Class.h" void MyFunction(char*, char*, CClass*);:-D

        G 1 Reply Last reply
        0
        • N Nynaeve

          class CClass; // OR: #include "Class.h" void MyFunction(char*, char*, CClass*);:-D

          G Offline
          G Offline
          georgiek50
          wrote on last edited by
          #4

          Ok, thanks for the reply...I must omit "class" before the class. NOw on to the compile errors, I don't understand them because I am including the header file that contains the class definition. Isn't this enough?

          T A 2 Replies Last reply
          0
          • G georgiek50

            Ok, thanks for the reply...I must omit "class" before the class. NOw on to the compile errors, I don't understand them because I am including the header file that contains the class definition. Isn't this enough?

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            what are the errors now ? compile error ? what is the description of the error ? what is the line giving the error ?


            TOXCCT >>> GEII power

            1 Reply Last reply
            0
            • G georgiek50

              Ok, thanks for the reply...I must omit "class" before the class. NOw on to the compile errors, I don't understand them because I am including the header file that contains the class definition. Isn't this enough?

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              that is right. Do not use the word class before variable declarations, only before the class declaration. class B1 int i; B1 b; // B1 is a type like int is a type Your compile errors should be discernable. Take a break and then look at them again. To make it more fun, Java has a "Class" type and this can be used in declaring a variable such as Class somvarthatwillrefertoaclasslater; // :-)

              T 1 Reply Last reply
              0
              • A Anonymous

                that is right. Do not use the word class before variable declarations, only before the class declaration. class B1 int i; B1 b; // B1 is a type like int is a type Your compile errors should be discernable. Take a break and then look at them again. To make it more fun, Java has a "Class" type and this can be used in declaring a variable such as Class somvarthatwillrefertoaclasslater; // :-)

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                Anonymous wrote: Java has a "Class" type and this can be used in declaring a variable such as Class somvarthatwillrefertoaclasslater; are you sure it is not a declaration before a definition instead of a type ?! (in C++, it is like that...)


                TOXCCT >>> GEII power

                G 1 Reply Last reply
                0
                • T toxcct

                  Anonymous wrote: Java has a "Class" type and this can be used in declaring a variable such as Class somvarthatwillrefertoaclasslater; are you sure it is not a declaration before a definition instead of a type ?! (in C++, it is like that...)


                  TOXCCT >>> GEII power

                  G Offline
                  G Offline
                  georgiek50
                  wrote on last edited by
                  #8

                  Thanks for the replies guys. The error line reads: error C2061: syntax error : identifier 'cDataFile' Where cDataFile is a class defined in another header file which I have included in this header file The function definition (part of another class) void Save(cDataFile*); So I'm stumped :confused:

                  N 1 Reply Last reply
                  0
                  • G georgiek50

                    Thanks for the replies guys. The error line reads: error C2061: syntax error : identifier 'cDataFile' Where cDataFile is a class defined in another header file which I have included in this header file The function definition (part of another class) void Save(cDataFile*); So I'm stumped :confused:

                    N Offline
                    N Offline
                    Nynaeve
                    wrote on last edited by
                    #9

                    First, make sure that cDataFile is spelled correctly (including capitalization, I am suspicious of your lowercase 'c'). Second, when your function uses a pointer to a class, as you did, you only need to forward include the class in the header file. Then put the #include in the .cpp file (not the .h file) i.e. class cDataFile; class CYourClass { void Save(cDataFile*); };

                    G 1 Reply Last reply
                    0
                    • N Nynaeve

                      First, make sure that cDataFile is spelled correctly (including capitalization, I am suspicious of your lowercase 'c'). Second, when your function uses a pointer to a class, as you did, you only need to forward include the class in the header file. Then put the #include in the .cpp file (not the .h file) i.e. class cDataFile; class CYourClass { void Save(cDataFile*); };

                      G Offline
                      G Offline
                      georgiek50
                      wrote on last edited by
                      #10

                      Thanks for the continued help...I seem to be a bit confused though...the spelling is correct (I always use a lowercase c for classes) Here is my file structure/layout: Header1.h --------- class cDataFile { ... }; ---------- Header2.h --------- #include "Header1.h" class cSomeClass { public: void SaveSomeFile(cDataFile*); <---- This is were I get the error }; ---------- Could you please explain what you said before in this semi-diagramatical way? Thanks.

                      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