Public Group

# Getter and setter problem (c++)

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

## Recommended Posts

I'm trying to access and change the information stored in one class (my baseship class) and have it display in another (ship builder class). I know that I need to use getters and setters to do this (come from a c#/xna background) as I will also need to change to information stored in the baseship class as well.

My base ship .h looks like this:

#pragma once #include <stdlib.h> class BaseShip { protected: public: BaseShip(void); ~BaseShip(void); int speed; //getters int GetSpeed(); //setters void SetSpeed(int speed); };

the .cpp

#include "BaseShip.h" #include <stdlib.h> BaseShip::BaseShip(void) { } BaseShip::~BaseShip(void) { } void BaseShip::SetSpeed(int speed) { this->speed = speed; } int BaseShip::GetSpeed() { return speed; }

Now in the class I'm trying to access it I add in the include to the baseship.h file in the required classes.h. Then I create a pointer like:

BaseShip *ship;

The only problem is, when I try to use ship in the class I don't get access to my set or get methods (only need it to return/display a number). All I get is the ~BaseShip.

Can anyone see what it is I'm doing wrong?

##### Share on other sites
Are you actually getting a error message when you compile? You're class looks all right, with the obvious that "int speed" is declared public and you wouldn't even need to use the accessor/mutator. Just need a little more information

##### Share on other sites
Hidden

Can anyone see what it is I'm doing wrong?

Let's just say I wouldn't rely too heavily on Visual Studio's Intellisense for C++*, as C++ is notoriously difficult to parse; it's a miracle Intellisense can even be correct some of the time.

##### Link to post

I'm trying to access and change the information stored in one class (my baseship class) and have it display in another (ship builder class). I know that I need to use getters and setters to do this (come from a c#/xna background) as I will also need to change to information stored in the baseship class as well.

My base ship .h looks like this:

#pragma once #include <stdlib.h> class BaseShip { protected: public: BaseShip(void); ~BaseShip(void); int speed; //getters int GetSpeed(); //setters void SetSpeed(int speed); };

the .cpp

#include "BaseShip.h" #include <stdlib.h> BaseShip::BaseShip(void) { } BaseShip::~BaseShip(void) { } void BaseShip::SetSpeed(int speed) { this->speed = speed; } int BaseShip::GetSpeed() { return speed; }

Now in the class I'm trying to access it I add in the include to the baseship.h file in the required classes.h. Then I create a pointer like:

BaseShip *ship;

The only problem is, when I try to use ship in the class I don't get access to my set or get methods (only need it to return/display a number). All I get is the ~BaseShip.

Can anyone see what it is I'm doing wrong?

Baseship* ship only defines a pointer in C++, that is a variable that points at another memory location in which a class of that type is actually stored.

Baseship ship; on the other hand defines a variable that is a ship.

 Baseship* ship = new BaseShip(); //This works and ship is valid Baseship shipTwo(); //This works Baseship* shipThree; //Not initialise so points at a random memory location //shipThree->setSpeed(0); //This might crash and it might not depending on where shipThree->speed is pointing. shipThree = &shipTwo; //ShipThree will now modify vars in shipTwo shipThree->setSpeed(500); //Changes shipTwo.speed to 500 shipThree = ship; //ShipThree will now modify vars in ship shipThree->setSpeed(500); //(*ship).speed now is 500 delete ship; //Don't forget to delete variables you new 

##### Share on other sites
Your speed is also public, so you don't need getters and setters unless you make it protected/private.

##### Share on other sites
@way2lazy - I know mate. I was just testing something. Plan on making all the variables protected. Thanks though

Well I tired this:

ship->setSpeed(500);

and get the error:

error C2039: 'setSpeed' : is not a member of 'BaseShip'

Then I tried:
shipTwo->setSpeed(10);

error:

left of '->setSpeed' must point to class/struct/union/generic type

They are set up like this:

BaseShip* ship = new BaseShip();
BaseShip shipTwo();

Wanted to test both ways.

When I hold over the mouse where the red line appears for an error at setSpeed I get the following:

BaseShip has no member setSpeed.

Any ideas?

##### Share on other sites
Not sure if it's a typo in your post, but you are listing setSpeed for the setter called SetSpeed

if the above doesn't apply, make sure you #include BaseShip.h. When I see:
[color=#CCCCCC][size=2]left of '->setSpeed' must point to class/struct/union/generic type
[color=#CCCCCC][size=2]

[color="#CCCCCC"][size=2][color=#000000]

### for me, not including the header file is usually why...

##### Share on other sites
It's because you're calling "setSpeed" ( notice lowercase 's' on set ), when you declared it "SetSpeed". The reason you're getting the issue with shipTwo, is because you're not declaring it a pointer so you cant use the "->" to access it's members. You use a normal '.',

shipTwo.SetSpeed( 10 );

##### Share on other sites
A little side tip. Add "const" to the end of any getter function or other functions that do not modify the class. This tells the compiler "calling this method will not change the state of this class", which can be useful down the road to enhance the encapsulation of your class.

 //getters int GetSpeed() const; 

##### Share on other sites
Another note, since you didn't include the source of your derived class, are you expecting these to be virtual functions?

If you want the GetSpeed function (or any other function) to be replaced by a child class, you need to mark it as virtual in the base.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631444
• Total Posts
3000100
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!