Jump to content
  • Advertisement
Sign in to follow this  
Side Winder

(C++) Having a problem calling an accessor function

This topic is 3976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a base class, with a few classes that derive from this. One of the protected variables in the base class is a variable for rotation. Now, two of the derived classes are Bullet and Ship. In one of the function of the Bullet class I want to find the current Ship rotation, but when I try and use my accessor function (from the base class) I get this error:
Quote:
error C2597: illegal reference to non-static member 'PositionedObject::m_Rotation'
How do I get around this, i.e. how do I access the Ship class rotation variable from my Bullet class? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You are trying to access a non-static member (an instance member) from a static method, like this:

class Example
{
public:
static void FunctionA ()
{
cout << m_non_static_member; // won't work as there is no 'this' pointer
// in static methods
}

private:
int
m_non_static_member; // can only be accessed through a 'this'/instance pointer
};

Skizz

Share this post


Link to post
Share on other sites
I haven't specifically assigned any of my functions as static though... Or do object functions automatically become static when created? This is what I have (basically):

class PositionedObject
{
protected:
float m_Rotation;

public:
float GetRotation() { return m_Rotation; }

};


class Ship : public PositionedObject
blablabla


class Bullet : public PositionedObject
public:

void Fire()
{
m_Rotation = Ship::GetRotation();
}


Now, I guess this is downright wrong anyway, so how do I go about fixing it?

Share this post


Link to post
Share on other sites
Ah.. Yes, fair point. In that case, how can I do this? I need to be able to find the Ship's current rotation so that the bullet is then facing the correct direction when fired...

Share this post


Link to post
Share on other sites
You need to instantiate your ship and store it somewhere and then when you need to get THAT ship's rotation you use THAT instance. ie


std::vector<Ship> shipList;

// add elements into the list however you wish.

// .. later
Ship curShip = shipList[index];
float rotation = curShip.GetRotation();


Or something similar. Highly simplified of course, but hopefully you'll get the idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by Side Winder
Ah.. Yes, fair point. In that case, how can I do this? I need to be able to find the Ship's current rotation so that the bullet is then facing the correct direction when fired...


You have the same bug in your English description as in the program. The current rotation of which Ship?

Classes are not a storage mechanism for code and data; they define a data type. You should think of 'Ship' in almost exactly the same way you think of 'int'.

If you wanted to add 3 to an int in your function, what techniques would you have for specifying what int to add 3 to*? The same ideas apply here.

(* Possible answers: create it locally; pass it in as a parameter - by reference, if you need to use the caller's instance directly rather than a copy, or work with a global - not usually a good idea; use a data member of the current object - but in this case, it makes absolutely zero sense for a bullet to contain a Ship; use a data member in the current object - usually a pointer - which *refers to* the thing to change - but you might not want to "associate" bullets with their targets so heavy-handedly.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Side Winder
Ah.. Yes, fair point. In that case, how can I do this? I need to be able to find the Ship's current rotation so that the bullet is then facing the correct direction when fired...


You have the same bug in your English description as in the program. The current rotation of which Ship?

Classes are not a storage mechanism for code and data; they define a data type. You should think of 'Ship' in almost exactly the same way you think of 'int'.

If you wanted to add 3 to an int in your function, what techniques would you have for specifying what int to add 3 to*? The same ideas apply here.

(* Possible answers: create it locally; pass it in as a parameter - by reference, if you need to use the caller's instance directly rather than a copy, or work with a global - not usually a good idea; use a data member of the current object - but in this case, it makes absolutely zero sense for a bullet to contain a Ship; use a data member in the current object - usually a pointer - which *refers to* the thing to change - but you might not want to "associate" bullets with their targets so heavy-handedly.)


When I said "the Ship", I meant the object. It's one of a kind, hence the noun. Right now I'm trying to get my head around the whole concept of encapsulation (and OOP in general) within games and not having so many functions in my Main.cpp

Share this post


Link to post
Share on other sites
Quote:
Original post by Side Winder
When I said "the Ship", I meant the object. It's one of a kind, hence the noun.


No, right now it's zero of a kind. You haven't made a ship yet. You've just described what ships are like.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Side Winder
When I said "the Ship", I meant the object. It's one of a kind, hence the noun.


No, right now it's zero of a kind. You haven't made a ship yet. You've just described what ships are like.


Hm? When I originally posted, yes, true. However, when I was talking about finding the ship's current rotation, I did mean the current (already made) object... Either way, I decided on taking the parameter route... The only problem is is that the function is currently not part of any class, which I must now sort out!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!