need help trying to call a class function

Hello all

I have a class with its own get and set functions in it. I'm trying to get the user to input a value, then pass that value into a set function. But its giving me an error when I try to call that set function. Heres the code i'm trying to do:

1
2
3
4
5
out << "How much are you putting in?" << endl;
	cout << "$";
	cin >> getdollars;
	cout << endl;
	setdollars(getdollars);


I also tried
1
2
3
4
5
out << "How much are you putting in?" << endl;
	cout << "$";
	cin >> getdollars;
	cout << endl;
	retirement::setdollars(getdollars);


But they both give me an error

Am I calling the function correctly? setdollars is public, and I copied and pasted it from a few lines above.

the function I'm working in, is outside the class, but i'm not sure how that makes much difference.

If anyone knows what i'm doing wrong, i would apreciate any help.

edit: spelling errors
Last edited on
> But they both give me an error
insufficient data for meaningful answer
Am I calling the function correctly?
Depends. If it is member function then no. If it is freestanding function then I do not see where there is mention of object value would be assigned to
fair enough, the function i'm working in is its own void function. and has no ties to the class i'm referencing. So i'm working in an independent void function, getting the user input data to a variable, then trying to pass that data to a public function in a class.

and the error i'm getting is: 'a nonstatic member must be relative to a specific object' when I hover over it. in the error log it says: error C2352: 'retirement::setdollars' : illegal call of non-static member function.

could the problem be that i'm working in a void function?

do you want to see the whole function? its more of the same with local variables for the user to store data before passing it into the assignment functions from the class. But I'll be more then happy to show you what i'm working with.
do you want to see the whole function?
It would be neat. Because your compiler tells you that it is not freestanding function, but member of class.
fair enough here is my public parts of the class and the 2 functions outside of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	retirement();
	retirement(double R, double m, double r, double t, double totaled);

	void setdollars(double setdol);
	double getdollars();

	void setdeposits(double setdepo);
	double getdeposits();

	void setinterest(double setint);
	double getinterest();

	void setyears(double sety);
	double getyears();

	void settotal(double settot);
	double gettotal();

};
void getinputs();
double mathfunction(double R, double m, double r, double t, double totaled);


and heres the whole function i'm working with:
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
void getinputs()
{
	double getdollars, getdepos, getinterest, getyears;

	cout << "How much are you putting in?" << endl;
	cout << "$";
	cin >> getdollars;
	cout << endl;
	retirement::setdollars(getdollars);

	cout << "How many deposits are you going to do?" << endl;
	cout << "#";
	cin >> getdepos;
	cout << endl;

	cout << "Whats the interest rate?" << endl;
	cout << "%";
	cin >> getinterest;
	cout << endl;

	cout << "How many years are you saving for?" << endl;
	cin >> getyears;
	cout << endl;


}


and heres the function i'm calling, just a few lines above the afore mentioned function in the same .cpp file:

1
2
3
4
void retirement::setdollars(double setdol)
{
	dollars = setdol;
}


anything else you need or are curious about?
Last edited on
retirement::setdollars(getdollars); You need object which method you will call. Now you are trying to call method as it was static.
if i'm understanding you correctly (and i'm not sure that I am) i'm trying to get the fuction to return a value. do I have that right?

If so, how do I pass in the getdollars value?
No, you have to create object. You have a class. It is leke a car blueprint. Can you drive blueprint? No, you need actual object. You don't do cout << int, you will do int x = 5; cout << x; So you should do the same for your class.
> i'm trying to get the fuction to return a value
¿what function? ¿those declared as "I don't return a value" (aka void)?


You need to work on an object
1
2
3
4
5
6
int main(){
   retirement regular, disability, early;
   regular.setdollars( 1 );
   disability.setdollars( 0.5 );
   early.setdollars( 100 );
}
ok, if I'm understanding you, I need a class variable. do I have that right?

Something like 'retirement i;'

The goal of this program is to take all the user inputs and then mathematically figure out how much money they'll have after the amount of years they enter.

just as an example, they input something like

10
2
.05
30

Then plug all that information into a formula and spit out the answer. I was given a formula to work with, but I don't think I can represent it here very well.
that worked! awesome, thanks all!

Now another question, how can I pass in values from the class to math function. Please keep in mind I'm not using the main function yet. I'm trying to get away from using the main for anything but function calls. I did wind up using the retirement i variable. Why? to keep it simple, that's why.

Can I pass in i by reference? I'm a little hesitant to ask that, because i've only ever passed in a variable by reference once a few years back. and its been around a year since I even used a function to return any value outside of classes.
Topic archived. No new replies allowed.