So I need my objects to be able to use functions that are within the main class, so when they are created I want them to get it through their parameters.
int main(int argc, char* args[])
{
Unit units[3]={{5,5,this},{8,8,this},{12,12,this}};
units[0].init(true);
for (int i=1;i<sizeof(units) / sizeof(units[0]);i++) {
units[i].init(false);
}
Where i put “this” it should be the main class, this is how I would do it in java but I am unsure how to do it here. I tried “*this” and “this” but all I get is an error:
Invalid use of ‘this’ in non-member function.
Searching for the error gave me nothing to work with since I am rather unknowing on c++’s class systems.
The two first parameters are for the location. The Init commands parameter sets whether they are allies or not.
I want the Unit classes to be able to access:
int getClosestHostileX(int ask_x,int ask_y,bool team) {
return 55;
}
There is supposed to be more code here later I am just trying to get them to return.
I am using Code::Blocks IDE and GNU GCC compiler.
TL;DR How do I make my other classes access functions from my main class?
You couldn’t do that in Java, either. In Java, the entrypoint is a static method and has no associated object instance.
The solution is the same — instantiate your type.