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. calling a static function

calling a static function

Scheduled Pinned Locked Moved C / C++ / MFC
help
32 Posts 7 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.
  • T toxcct

    Mark Salsbery wrote:

    If it wasn't allowed

    shouldn't it be: "if it weren't allowed" ? :confused: :)

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #4

    :laugh: Probably. I'm a software engineer, dammit, not an English professor. :)

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    T 1 Reply Last reply
    0
    • M Mark Salsbery

      :laugh: Probably. I'm a software engineer, dammit, not an English professor. :)

      Mark Salsbery Microsoft MVP - Visual C++ :java:

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

      f*ck that, i'm french, not a native english speaker (like you) ! lol

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      M 1 Reply Last reply
      0
      • T toxcct

        Mark Salsbery wrote:

        If it wasn't allowed

        shouldn't it be: "if it weren't allowed" ? :confused: :)

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

        Not necessarily.

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

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        1 Reply Last reply
        0
        • M Mark Salsbery

          steph5 wrote:

          it is being called from another source file in the project! I can't see any reason why it is aloud to do this.

          If it wasn't allowed, then all our source code would have to be in one file. Thankfully we have a linker to link separate compiled code modules :)

          steph5 wrote:

          In fact I have added my own class and function looking exactly the same and it gives me LNK 2019 error.

          To do its job, the linker needs to know where to find the code to link to. What's the complete error you're getting? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S Offline
          S Offline
          steph5
          wrote on last edited by
          #7

          Hi I was in the understanding that if you declare a static function it can only be called from the source file where it is declared. Am I missing something?? The error says "unresolved external symbol" the call to the function is in file1.cpp where I have: myClass::myFunction(variable1, variable2); In myClass.h I have: class myClass { static void myFunction } and in myClass.cpp I have the function definition: myClass::myFunction() { //function definition } I can't call myFunction from file1.cpp As you can tell I am a novice programmer and in desperate need of explanation :(

          M C 2 Replies Last reply
          0
          • T toxcct

            f*ck that, i'm french, not a native english speaker (like you) ! lol

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #8

            toxcct wrote:

            not a native english speaker (like you)

            LOL! I'm pretty sure the rest of the world doesn't call what we Americans speak "English" ;P

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            J 1 Reply Last reply
            0
            • T toxcct

              Mark Salsbery wrote:

              If it wasn't allowed

              shouldn't it be: "if it weren't allowed" ? :confused: :)

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #9

              Now that DavidCrow mentions it, and the caffeine is kicking in... I'm pretty sure I was right ;P LMAO

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • T toxcct

                Mark Salsbery wrote:

                If it wasn't allowed

                shouldn't it be: "if it weren't allowed" ? :confused: :)

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #10

                Nope. ;)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                T 1 Reply Last reply
                0
                • S steph5

                  Hi I was in the understanding that if you declare a static function it can only be called from the source file where it is declared. Am I missing something?? The error says "unresolved external symbol" the call to the function is in file1.cpp where I have: myClass::myFunction(variable1, variable2); In myClass.h I have: class myClass { static void myFunction } and in myClass.cpp I have the function definition: myClass::myFunction() { //function definition } I can't call myFunction from file1.cpp As you can tell I am a novice programmer and in desperate need of explanation :(

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #11

                  steph5 wrote:

                  I was in the understanding that if you declare a static function it can only be called from the source file where it is declared. Am I missing something??

                  Yes :) That would make calling any external functions impossible.

                  steph5 wrote:

                  I can't call myFunction from file1.cpp

                  The linker can't find myClass.obj. Is myClass.cpp not part of the project? Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • S steph5

                    Hi I was in the understanding that if you declare a static function it can only be called from the source file where it is declared. Am I missing something?? The error says "unresolved external symbol" the call to the function is in file1.cpp where I have: myClass::myFunction(variable1, variable2); In myClass.h I have: class myClass { static void myFunction } and in myClass.cpp I have the function definition: myClass::myFunction() { //function definition } I can't call myFunction from file1.cpp As you can tell I am a novice programmer and in desperate need of explanation :(

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #12

                    steph5 wrote:

                    I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                    That's true.

                    steph5 wrote:

                    Am I missing something??

                    Yes: in

                    steph5 wrote:

                    class myClass { static void myFunction }

                    is not an ordinary (i.e. C-style) static function, is a static method (i.e is a member of the class even if you don't need an instance of the class to call it). :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    M S R 3 Replies Last reply
                    0
                    • C CPallini

                      steph5 wrote:

                      I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                      That's true.

                      steph5 wrote:

                      Am I missing something??

                      Yes: in

                      steph5 wrote:

                      class myClass { static void myFunction }

                      is not an ordinary (i.e. C-style) static function, is a static method (i.e is a member of the class even if you don't need an instance of the class to call it). :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #13

                      CPallini wrote:

                      That's true.

                      Huh? I call static methods that aren't in the same source file all the time :)

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      S C 2 Replies Last reply
                      0
                      • C CPallini

                        steph5 wrote:

                        I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                        That's true.

                        steph5 wrote:

                        Am I missing something??

                        Yes: in

                        steph5 wrote:

                        class myClass { static void myFunction }

                        is not an ordinary (i.e. C-style) static function, is a static method (i.e is a member of the class even if you don't need an instance of the class to call it). :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        S Offline
                        S Offline
                        steph5
                        wrote on last edited by
                        #14

                        Any ideas how I can have a function I can call from anywhere but where I don't need an instance of the class to call it?

                        M 1 Reply Last reply
                        0
                        • S steph5

                          Any ideas how I can have a function I can call from anywhere but where I don't need an instance of the class to call it?

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #15

                          You can call a static method of a class from other cpp files! You need to link the files however. The problem is in your project - the linker isn't finding the file with the function being called. Fix that and it will work. The two files in your example ARE in the same project, right?

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          S 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            CPallini wrote:

                            That's true.

                            Huh? I call static methods that aren't in the same source file all the time :)

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            S Offline
                            S Offline
                            steph5
                            wrote on last edited by
                            #16

                            how?

                            M 1 Reply Last reply
                            0
                            • S steph5

                              how?

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #17

                              ////////////////////////////
                              // myClass.h
                              ////////////////////////////

                              class myClass
                              {
                              public:
                              static void StaticMethod();
                              };

                              ////////////////////////////
                              // myClass.cpp
                              ////////////////////////////

                              void myClass::StaticMethod()
                              {
                              }

                              ////////////////////////////
                              // someother.cpp
                              ////////////////////////////

                              void somefunc()
                              {
                              myClass::StaticMethod();
                              }

                              Maybe you forgot the part in red above...

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              1 Reply Last reply
                              0
                              • M Mark Salsbery

                                You can call a static method of a class from other cpp files! You need to link the files however. The problem is in your project - the linker isn't finding the file with the function being called. Fix that and it will work. The two files in your example ARE in the same project, right?

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                S Offline
                                S Offline
                                steph5
                                wrote on last edited by
                                #18

                                yes all the files are incluceded in the same project. how do I make sure the linker finds the file? The thing is I can call myClass::myFunction() from one of the .cpp files in the project, but I can't call it from another .cpp file in the same project. whats going on? Thanks for your time

                                1 Reply Last reply
                                0
                                • C CPallini

                                  Nope. ;)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

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

                                  you're saying this as if such construction never exists... but I'm sure it is ! for example, the following is perfectly valid : "If I were you, I wouldn't do that"... ;P

                                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                  M C 2 Replies Last reply
                                  0
                                  • M Mark Salsbery

                                    toxcct wrote:

                                    not a native english speaker (like you)

                                    LOL! I'm pretty sure the rest of the world doesn't call what we Americans speak "English" ;P

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    J Offline
                                    J Offline
                                    john john mackey
                                    wrote on last edited by
                                    #20

                                    touché :laugh:

                                    1 Reply Last reply
                                    0
                                    • T toxcct

                                      you're saying this as if such construction never exists... but I'm sure it is ! for example, the following is perfectly valid : "If I were you, I wouldn't do that"... ;P

                                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                      M Offline
                                      M Offline
                                      Mark Salsbery
                                      wrote on last edited by
                                      #21

                                      Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P

                                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                                      T 1 Reply Last reply
                                      0
                                      • M Mark Salsbery

                                        CPallini wrote:

                                        That's true.

                                        Huh? I call static methods that aren't in the same source file all the time :)

                                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                                        C Offline
                                        C Offline
                                        CPallini
                                        wrote on last edited by
                                        #22

                                        Actually his sentence,

                                        steph5 wrote:

                                        I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                                        and yours

                                        Mark Salsbery wrote:

                                        Huh? I call static methods that aren't in the same source file all the time

                                        are not in conflict: standard (i.e C-like, not belonging to a class) static functions have file scope, while static methods have not such a constraint. :)

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                        [My articles]

                                        T M 2 Replies Last reply
                                        0
                                        • M Mark Salsbery

                                          Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P

                                          Mark Salsbery Microsoft MVP - Visual C++ :java:

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

                                          Mark, wait. I didn't say all I write is gramatically correct; I make mistakes sometimes. BUT, isn't the construction I just asked you in this thread valid ?

                                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                          M 2 Replies 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