Class Errors

Hello Everyone!

I am having some problems with declaring a class and am getting many errors.

Here is my problem:

I must write a program that enables the user to enter a basketball player's name and his floowing raw statistics: free throws attempted, free throws made, field goals attempted, field goals made, rebounds, assists, steals, blocked shots, and games played. Then I must calculate free throw percentage, field goal percentage, points per game, rebounds per game, assists per game, steals per game, and blocked shots per game.

My program must include:
1. A constructor WITH DEFAULT VALUES to initialize the new class object.
2. A public member function to reset the raw statics.
3. Private member functions to perform the calculations for the finished statistics.
4. Get functions that return all per game averages.
5. A destructor which simply displays the message "End of THIS object!".

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

class Basketball {
private:
	float FTAttempted, FTMade, FGAttempted, FGMade, Rebounds, Assists, Steals, BlockedShots, GamesPlayed;
	void calcFTP() { ((FTAttempted/FTMade)*100); }
	void calcFGP() { ((FGAttempted/FGMade)*100); }
	void calcPPG() { (((FTMade*1) + (FGMade*2)) / GamesPlayed);}
	void calcRPG() { (Rebounds/GamesPlayed); }
	void calcAPG() { (Assists/GamesPlayed); }
	void calcSPG() { (Steals/GamesPlayed); }
	void calcBPG() { (BlockedShots/GamesPlayed); }
public:
	 void setData(float FTA, float FTM, float FGA, float FGM, float R, float A, float S, float B, float G);
	 Basketball(float FTA =0, float FTM=0, float FGA=0, float FGM=0, float R=0, float A=0, float S=0, float B=0, float G=0)
		{setData(FTA, FTM, FGA, FGM, R, A, S, B, G);}
	 float getFTP() {return calcFTP();}
	 float getFGP() {return calcFGP();}
	 float getPPG() {return calcPPG();}
	 float getRPG() {return calcRPG();}
	 float getAPG() {return calcAPG():}
	 float getSPG() {return calcSPG();}
	 float getBPG() {return calcBPG();}
	 ~Basketball() {cout << "End of THIS object!\n";}
};

void Basketball::setData(float FTA, float FTM, float FGA, float FGM, float R, float A, float S, float B, float G)
{
	FTAttempted=FTA;
	FTMade=FTM;
	FGAttempted=FGA;
	FGMade=FGM;
	Rebounds=R;
	Assists=A;
	Steals=S;
	BlockedShots=B;
	GamesPlayed=G;
}

int main()
{
} 
What are the errors?
The errors that I am currently getting are within the public get functions. I am getting "error C2440: can't convert from void to float". I have seven of those since there are seven functions :p.
if you call

 
     return getXXX();


are you aware that the compiler expects to find a float value from getXXX()? But in fact you declare

 
void getXXX();


Do you know what to change now? :-)
If you still have problems to get rid of these error messages, feel free to ask, but i recommend you to consider some tutorials on functions in C/C++.

yours Maikel
So i would need to change the private functions from void to float then? Because I can't change the public get functions to void because they are returning something....atleast that is what I am getting from your last post.
See if your function is declared to return a float, then you need someting like
1
2
3
4
5
float cool_func()
{
    float i_am_float = 1.0f;
    return(i_am_float);
}


so if you now substitute 'i_am_float' with a function (this is legitimate)

1
2
3
4
5
6
float another_func() { return 1.0f; }

float cool_func()
{
    return(another_func());
}


we should now expect that 'another_func()' gives back a float value, that we then can return as it would be a normal value like 'i_am_float'. But in your case you declared at this point 'another_func()' as a 'void function' something like

1
2
3
4
5
6
void i_return_nothing { return(); /* hahahaha! give you nothing, all MINE! */ }

float cool_func()
{
    return(i_return_nothing());
}


At this point C++ thinks you are joking with it, and dont want to compile you anything, because he expected a float and found nothing (meaning of void).

So yes, just change your private funs from void to float.
I hope i could help, but I think it would really help you to read more about functions, variables and so on. You will get a powerfull weapon your side.

Yours, Maikel
Last edited on
Thanks for all the help Maikel!
Topic archived. No new replies allowed.