calling a static function
-
Hi I am looking at someone elses project and source code and seeing something I need explaining! There is a STATIC member function declared in a class. eg. class myClass { static void myFunction(); } Then it is called using the scope resolution opperator. myClass::myFunction() but it is being called from another source file in the project! I can't see any reason why it is aloud to do this. In fact I have added my own class and function looking exactly the same and it gives me LNK 2019 error. Any Ideas what I am missing. Thanks in advance for your time. :doh:
-
Hi I am looking at someone elses project and source code and seeing something I need explaining! There is a STATIC member function declared in a class. eg. class myClass { static void myFunction(); } Then it is called using the scope resolution opperator. myClass::myFunction() but it is being called from another source file in the project! I can't see any reason why it is aloud to do this. In fact I have added my own class and function looking exactly the same and it gives me LNK 2019 error. Any Ideas what I am missing. Thanks in advance for your time. :doh:
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:
-
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:
Mark Salsbery wrote:
If it wasn't allowed
shouldn't it be: "if it weren't allowed" ? :confused: :)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Mark Salsbery wrote:
If it wasn't allowed
shouldn't it be: "if it weren't allowed" ? :confused: :)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
:laugh: Probably. I'm a software engineer, dammit, not an English professor. :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
:laugh: Probably. I'm a software engineer, dammit, not an English professor. :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
f*ck that, i'm french, not a native english speaker (like you) ! lol
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Mark Salsbery wrote:
If it wasn't allowed
shouldn't it be: "if it weren't allowed" ? :confused: :)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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
-
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:
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 :(
-
f*ck that, i'm french, not a native english speaker (like you) ! lol
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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:
-
Mark Salsbery wrote:
If it wasn't allowed
shouldn't it be: "if it weren't allowed" ? :confused: :)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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:
-
Mark Salsbery wrote:
If it wasn't allowed
shouldn't it be: "if it weren't allowed" ? :confused: :)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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] -
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 :(
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:
-
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 :(
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] -
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]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:
-
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] -
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?
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:
-
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:
-
////////////////////////////
// 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:
-
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:
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
-
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]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]
-
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:
touché :laugh: