I am getting an error when I call one static function of a class in to any other function (means by non-class function), then it is giving following error:
undefined reference to function name_function.
Can any one tell me why this is happing?
Basic C++: A non-static member function assumes that it has access to an object of the class type (it can refer to the member variables directly and the "this" pointer points to the object).
This means that you can't call a non-static member function unless you have an object of that type. In a static member function, you don't.
(I'm not 100% sure this is an answer to your question, as it's not clean from your explanation if you tried to call a non-static function from a static one, or vise versa.)
Are you prepending the class name before the function name?
so, if you have:
class MyClass
{
...
public static function name_function() { ... }
...
}
you need to call this function like this:
MyClass::name_function();
Related
I am building a class Boggle in C++. In the Boggle class, I have declared a structure type called boardIndex:
struct Boggle::boardIndex {
int row, col;
};
And a callback function to compare two "boardIndex"s:
int Boggle::CmpByIndex(boardIndex a, boardIndex b)
I want to pass the callback function to a Set of boardIndex elements within the Boggle.cpp file:
Set<boardIndex> usedIndices(CmpByIndex);
Is this possible? In the current form, I'll get an error that a "reference to non-static member function must be called." I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?
I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?
If you do not have any objects then you cannot call non-static member function. So you have 2 solutions:
make this function either static or non member of class Boogie
create or somehow obtain an instance of Boogle and bind it to call of this method
I was reading before that static member functions of a class is similar to a global function. But this should depend on whether or not the static member function is public/private right?
For example:
class A
{
private:
static void aFunc();
};
int main()
{
A::aFunc();
}
In this case I wouldn't be able to call the static member function inside main() correct? If I instead had made it public, I can call it, just like a global function?
Yes, it does depend on the access modifier. If the function is public, then it is somewhat similar to a global function, but unlike a global function it has access to class members and has to be called with the class scope (prefixed with A:: in your example), to mention just a couple of differences.
BTW, when you call it in main(), you should just have
A::aFunc()
No need to stick void in there.
Yes, a static member function is similar to a non-member function in that you do not need to call it for an object of associated class type, but it obeys the usual class member access rules like a non-static member function.
In this case, you are correct to suggest that A::aFunc is inaccessible from main. However, your calling syntax is incorrect. You do not need to include the return type:
A::aFunc();
I'm reading sample code from an SDK, and I see this:
PXCSenseManager *senseManager = PXCSenseManager::CreateInstance();
Does this mean that CreateInstance() is a method in the PXCSenseManager class and that it is called?
Yup, CreateInstance() is a public static member function of class PXCSenseManager.
It means that the CreateInstance function in the namespace PXCSenseManager is being called. From the beginning of that line one can see that PXCSenseManager is actually some class. The nature of a function that creates instances and the way it is called with an explicit namespace makes it extremely likely that CreateInstance is a static member function of the PXCSenseManager class, but if this line of code comes from within a non-static member function there is a theoretical possible that CreateInstance is a non-static member function.
I have some strange scenario
I have object that pass to other object its 'this' pointer
like this :
void GameLayer::startGame()
{
m_pGameController = new GameController(this);
}
in the GameController constructor i set memeber with the GameLayer
GameController::GameController(GameLayer* gamelayer)
{
m_gamelayer = gamelayer;
}
in the GameController i have functions that using callback functions
like this :
GameController::methodA()
{
CurrentGem->runAction(GameController::mycallbackMethod);
}
In the callback function I access GameController functions and members with 'this'
for example:
GameController::mycallbackMethod()
{
int test = this->age();
std::string name = this->name();
}
But the problem is when I try to access the m_pGameController it gives me exception
that says its NULL pointer
GameController::mycallbackMethod()
{
this->m_gamelayer->someGamelayerMethod();
}
The error is :
Access violation reading location 0xFFFFFFFF.
What am I doing wrong ?
When passing your callback function do call like
CurrentGem->runAction(&GameController::mycallbackMethod);
Where mycallbackMethod should be static.
CurrentGem->runAction(GameController::mycallbackMethod);
make the function mycallbackMethod static.
Side note:
To call a member function by pointer, you need two things:
a pointer to the object (or object itself)
and a pointer to the function
because pointer to member function doesn't hold reall address of the function but only offset inside object. A static member function however has no this pointer, it is the same as a regular global function, except it shares the name scope of class with other class members. So in this case you don't need pointer to object to call such callback.
Instead of having static methods and passing around a pointer to the class instance, you could use functionality of std::function and std::bind.
Is there a way to call a non static class member method from another method that is contained within the main class in c++? If so, what would the code look like?
Problem is, I can't declare this specfic method as static, because it uses other methods within the same class that then don't work if I make the one static.
I'm trying to use:
MyClass::myClassMethod();
from a method within the main class, but it gives me the error: a non static member reference must be relative to a specific object.
To clarify, myClassMethod() uses other methods within MyClass like:
void myClassMethod() {
...
anotherClassMethod();
}
so if I were to make myClassMethod static it would interfere with calling anotherClassMethod().
What is the deal with calling non-static member function from a static member function?
Every non static member function is passed an this pointer implicitly in addition to the parameters you pass, the pointer passed is then dereferenced to refer class object members However static functions are not passed with the implicit thispointer and hence one cannot call any non static member function inside a static member function because there is no this to do so.
What is the solution, If you want to do it anyways?
You will need some mechanism to get the pointer to the object inside the static method and then you can call the member function using that pointer.
How to do that?
You will have to store the pointer to class object globally, or pass it as an instance in one of the function arguments to the static method.
However, both of above are workarounds, the important thing to note here is If you feel the need of calling a non static member function through a static member function then there is something wrong in your design.
On Second thoughts maybe I mis-read your Question before, Probably, Your question is:
How to call a non-static member function of a class from main?
You need a instance of the class to call non-static member functions.
So simply,
MyClass obj;
obj.myClassMethod();
And calling any other member function from within myClassMethod() would simply be:
void myClassMethod()
{
//...
anyOtherMyClassNonStaticMemberFunction();
//...
}
A static method is one that doesn't run on any particular object. It's a lot like a standalone function outside of a class, except that it's allowed to access private members in its class.
If you anotherClassMethod() is non-static, that means it has to be called on a specific object, an instance of the class. Because it's called on an object, it can access data stored in that object (non-static member variables). If myClassMethod() is static and you implement it as
void MyClass::myClassMethod() {
anotherClassMethod();
}
That won't work because anotherClassMethod() expects to be called on a specific object, but myClassMethod() doesn't have one. But if you know what object you want to call it on, you can do it as an ordinary method call on an object:
void MyClass::myClassMethod(MyClass &object) {
object.anotherClassMethod();
}
The object doesn't have to be passed in as an argument; it could be a static member variable in the class, for example:
class MyClass {
private:
static MyClass theInstance;
// ...
};
void MyClass::myClassMethod() {
theInstance.anotherClassMethod();
}
Ultimately, the question you need to ask yourself is: why is myClassMethod() static, and why is anotherClassMethod() non-static? Take a step back, think about what myClassMethod() is supposed to do. Does it make sense to call it when you don't have an instance to work with? If so, why does it need to call a method that expects to work with an instance?
The only way to call a non static method of a class is through an instance of that class. So you would need something like this...
MyClass myClass;
myClass.myClassMethod();
I think that maybe you could use the singleton pattern, keep a instance of the class in global. like a utility class.