Static member functions not acting as expected
I have the following code that is not compiling as expected. I have a class which has a static member function, and another class which includes its header trying to use it. It does not seem to be working.
static bool validLocation(int _x) // within class A
{
return false;
};
Class B includes class A, and has the following call in one of its functions:
if (!(A::validLocation(180)))
continue;
Obviously these are simplified for reading purposes, but why is this not acceptable?
Sorry开发者_如何转开发 for the vagueness. As for the error message:
"A::validLocation(int)", referenced from: B::functionThatCallsThis() in B.o
Symbols not found.
Collect2: ld returned 1 exit status
The static function is public, as declared in the header file.
From the error message you gave, I'm going to assume you aren't linking the objects together or you declared validLocation()
as a free function since I don't see a A::
before its definition. Hard to tell given the amount of information here.
Edit:
Now the error message is a bit more clear, and it looks like you aren't including the correct object files to the linker.
EDIT: Did you remember to link both A.o
and B.o
together to create your binary? It sounds like A.o
isn't getting included and the compiler didn't inline the static function.
Also you have a ;
at the end of your function that's definitely not needed and may be invalid (I can't recall)
when you define your function validLocation()
, are you defining in a separate file, .cpp perhaps? If so, you need to put A:: before the function name, like so:
static bool A::validLocation(int _x) // within class A
{
return false;
};
精彩评论