Getter and setter problem (c++)

Started by
8 comments, last by NightCreature83 12 years, 11 months ago
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?
Advertisement
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
Uziel was defeated by Tiny Mandragora.

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.

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Your speed is also public, so you don't need getters and setters unless you make it protected/private.
@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?
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...

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 );
Uziel was defeated by Tiny Mandragora.
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;

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

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.

This topic is closed to new replies.

Advertisement