Compilation error - Static member
I h开发者_开发知识库ave a class definition with a virtual method.
On compiling I get the error that 'MethodType Class::Method'
is not a static member of class Class
The most popular solution I have found is to add the keyword static to the Method definition in the header file.
However, the method is defined as virtual. So to add the static keyword I will have to remove the virtual keyword. Unfortunately that cannot be done as the class inherits from a parent where this method is also declared virtual, leading to another compiler error. (Please note, I'm using defined interfaces and have no access to the parent class's source code)
Does anyone have any ideas?
Header file:
class X : public OtherClass
{
public:
X();
~X();
virtual structType MethodName(ParamType1,ParamType2);
};
Then in the CPP file I have:
structType * X::MethodName(ParamType1 P1, ParamType2 P2)
{
//Implementation here
}
And that gets flagged with error:
'structType* X::MethodName' is not a static member of 'class X'
You have to make it static
if you want to call the method without an object of that class. This makes no sense for virtual methods.
You must create an object of that class, and then call the method.
struct X {
static int bar();
int foo();
};
X::bar(); // Works, static method called
X::foo(); // Doesn't work (your problem)
X x;
x.bar(); // Works, but X::bar() recommended (so that one sees that it's static...)
x.foo(); // Works, your solution
I think you have a parsing error.
Your class definition says:
class X : public OtherClass {
public:
X();
~X();
virtual structType MethodName(ParamType1,ParamTYpe2);
};
But your definition for MethodName
has a different return type:
structType* X::MethodName(ParamType1 P1, ParamType2 P2) {
//Implementation here
}
The compiler's not really sure what to make of that, and thinks you're trying to do something with a non-existent static member, for some reason.
The solution is to fix your function definition and declaration so that the function has one, single, consistent return type. Either structType
or structType*
.
Figured it out.
Finally. In the implementation of MethodName1
structType * X::MethodName(ParamType1 P1, ParamType2 P2)
ParamType1 was a non-standard type. It is actually a class in the underlying application to which I have only the API.
Turns out that the implemtation for class ParamType1 was missing / not compiled / something.
The problem with linking was not to do with the class I loaded, even though it would appear as such because of the line the compiler provided. So for future reference, when using classes and struct in a function definition, keep an eye out for this linking error.
Thanks for the assist again everyone.
精彩评论