C++ A nonstatic member reference must be relative to a specific object
Asked Answered
U

2

8
Vector2D tankPos = Tank_b017191c::GetTankPosition();

I am trying to call a function from a different class but I am getting this error:

47 IntelliSense: a nonstatic member reference must be relative to a specific object e:\Repos\GameAI\GameAI\PathFinder_b017191c.cpp 113 21 GameAI

I have included Tank_b017191c.h in my header file but not getting very far..

Unnatural answered 28/3, 2015 at 8:47 Comment(2)
You need an object to call a non-static member function. Object a; a.foo();Wispy
you need an instance of that class, not just its typeDumbhead
W
3

It seems that member function GetTankPosition is a non-static member function. You have to call it with using an instance of the class as for example

Tank_b017191c tank;
Vector2D tankPos  = tank.GetTankPosition();

or

Tank_b017191c tank( /* some arguments */ );
Vector2D tankPos  = tank.GetTankPosition();
Westbrook answered 28/3, 2015 at 8:55 Comment(3)
I tried this but then I get 'Error: no default constructor exists for class "Tank_b017191c"Unnatural
you are asking for the position of a tank . which one? have you created a tank to take the position of? if so, that's the variable you use.Dumbhead
@Unnatural It is just an example. You have to call the constructor (with parameters) that is defined in your class.Westbrook
E
1

You need to have something like this:

Tank_b017191c tank; // you first need to create an object of this class
Vector2D tankPos = tank.GetTankPosition();
Ectoblast answered 28/3, 2015 at 8:57 Comment(2)
I tried this but then I get 'Error: no default constructor exists for class "Tank_b017191c"Unnatural
This means your Tank_b017191c class has no default constructor and you have defined your own constructors. So you will now have to initialize that object with whatever constructor you would use to initialize an object of that class.Ectoblast

© 2022 - 2024 — McMap. All rights reserved.