accessing member functions from inside funtions (other than main)

Hi, i'm having trouble accessing custom class member functions while inside a non main function. i'm clearly missing fundamental here. any help would be appreciated.


my class in my header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  class Crew
	{
		
		public:
		void SetStrength (int mStrength) { Strength = mStrength;}
		int GetStrength () {return Strength; }
		void SetIntelligence (int mIntelligence) { Intelligence = mIntelligence;}
		int GetIntelligence () {return Intelligence;}
		void SetSpeed (int mSpeed) {Speed = mSpeed;}
		int GetSpeed () {return Speed;}
		private:
		int Strength;
		int Intelligence;
		int Speed;
		
	};


Now in the same header file i have a function, we'll call it sup(). in sup i want to access and possibly change the value of a member object. we'll call it CrewM1, but the following codes just won't work.

 
cout << CrewM1.GetStrength();


The error i get is about my compiler not knowing what CrewM1 is.
You do realize that your function implementation should not be in a header, it should be in a source file, right?

i have a function, we'll call it sup()

Show the code for this function.

Where and how did you define a variable named CrewM1? How did you pass this variable into your function?

no i did not realize that. I will move all my functions back to the main .cpp file tonight, but for now this is how i have it set up (as you asked about it). The class is defined in the header file (exact code provided in my last post).


main.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	
	srand(static_cast<unsigned int>(time(0))); 	//seed random number generator

	Crew CrewM1;
	Crew CrewM2;
	Crew CrewM3;
	Crew CrewM4;
	Crew CrewM5;

// some code here that isn't relevant to this
sup();


in the header file (that i now know doesn't belong here) i have all variables declared, prototypes for all functions and the functions themselfs. a watered down version of sup() is as follows.

1
2
3
4
5
6
sup()
{
      cout CrewM1.GetStrength();
      CrewM1.SetStrength (CrewM1.GetStrength + 1);
      cout CrewM1.GetStrength();
}


clearly what i want to do is display the value for Strength of the CrewM1 object of the Crew Class, than add 1 to is. Once that is done i want it to display the new value to confirm it.
While jlb is right, you should implement in the .cpp, it's not really of immediate concern, it won't actually cause compiler/runtime errors. Just keep in mind if you implement in your header file, and then you use that header file a lot, it can cause big bloated executable's and slow compile times. Why this happens may be best for you to learn another time, but feel free to do some reading on it when you get the chance.

Now, from what I can tell, you're trying to figure out how to access an instance of Crew in your function sup.

To do this, let sup take an instance of Crew as an argument, in this case a reference to one directly.

Your definition for sup would now look like this.

1
2
3
4
5
6
7
void sup(Crew & crew) // Notice the ampersand, we're asking for a reference! Not a copy!
{
    cout << crew.GetStrength() << endl;
    crew.SetStrength(crew.GetStrength() + 1);
    cout << crew.GetStrength() << endl;

}


Then to use it, you could do something like...
1
2
3
4
Crew CrewM1;
// some other stuff
// then later...
sup(CrewM1);
i was wondering if a reference would be what i'm looking for. Thanks for pointing me in the correct direction. I'll mess with it tonight and see if by using reference's will do the trick.

but this does bring up another question. why bother protecting call members if we can access them directly via reference's?
When you get the instance of crew by reference, its private members are still protected. Passing by reference is just to specify that you want the ACTUAL variable you passed, not a copy of it.

Now, with references you can do something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Foo
{
public:
    int & getBar() { return my_int; }
private:
    int my_int;
};

int main()
{
    Foo foo;
    int & int_reference = foo.getBar();
    // Uh oh, we now have unlimited access to a private member of foo now
}


That kind of code you definitely want to avoid. Never return a reference or pointer to a private member from a public function, unless you can use const correctness, which in the above example there isn't. You could fix that by doing...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Foo
{
public:
    const int & getBar() { return my_int; }
private:
    int my_int;
};

int main()
{
    Foo foo;
    int & int_reference = foo.getBar();
    // Wrong, int_reference isn't declared to be a const reference, so this won't even compile
}


If you return a const reference/pointer, then it's read only and your referenced data can't be addled with. If it's a class/struct, then only const qualified functions can be used, which in your case, you didn't const qualify any of your methods. You can apply that to your getters, i.e...

1
2
3
4
5
class Crew
{
public:
    int GetStrength() const { return Strength; }
    ...


Now your GetStrength() method guarentees that nothign can be mutated with GetStrength,
so if you have Crew as a const argument, you can still use GetStrength.
Thank you for taking the time to explain this to me. its funny how this little project of mine keep exactly in place with the book I'm reading. each chapter i read starts me down the path of the next think i will need.

I will have a more serious think about this tonight when i can look at my code in length and consider what your showing me here.
Turns out my problem was that i was creating the class objects inside main(). once i moved them to just before main everything started working.

Thank you all for the help.
Topic archived. No new replies allowed.