function used both in class and client code

Hi,

I have a helper function getNum() that I use as a generic way of accepting only numeric input from the console. I use this function in my class as a was of gathering input for part of a setter. I also use this function in my client code as a way to collect user input of a numeric menu option. How do I define the function properly so that I may use it in both the class member functions and the client code functions(The function is declared both above main and in the class header)?

I know of only three ways of making this work.

1. I could define the function twice. Once with the scope resolution going to the class and once without.

-This is not ideal because then it would technically be 2 different functions I think.

2. I could define it only in the class and then just always call it from an object of the class.

-This is not ideal because then I would have to give my client menu functions an object of the class for the sole purpose of calling this helper function which would not make sense.

3. I could define the function only once and make it a friend function of the class. (This is what I am currently doing).

-This is not ideal either because then the helper function will have access to private class members which it does not need.

This is the function in question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double getNum()
{
	char eaten;
	double num;

	cin >> num;
	while (!cin)
	{
		cout << "\nYou must enter an integer.\n\n==>  ";
				cin.clear();
		while ( cin.get(eaten) && eaten != '\n') 
			;
		cin.putback(eaten);
		cin >> num;
	}
	while ( cin.get(eaten) && eaten != '\n') 
		;
	cin.putback(eaten);
	return num;	
}
This function has nothing to do with a class so it should not be a member function. You can still call it from any member function you like. All you have to do is include the header that declares the function before using it.
It's just a function. If it were to be shared across programs, you'd put in a library. But as you're just using it in your program, put the prototype in a header file so the rest of the program can see it.

It's definitely not related to any class, so don't consider using friend. Just include the header file in the class' .cpp file and use it.
what about making the function as global .. and using it in client and server program
Peter and kbw,

If I do what you suggest, I get the following error on compile

1>Project4.obj : error LNK2019: unresolved external symbol "public: double __thiscall CTool::getNum(void)" (?getNum@CTool@@QAENXZ) referenced in function "public: void __thiscall CTool::setTool(int)" (?setTool@CTool@@QAEXH@Z)
1>E:\ITCS 2550\Project4\Debug\Project4.exe : fatal error LNK1120: 1 unresolved externals

This is my class header and client function declarations:

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
class CTool
{
	int record, quantity;
	char name[30];	
	double cost;
public:
	CTool (int r = 0, int q = 0, char n[30] = "", double c = 0.0) : record(r), quantity(q), cost(c) 
	{ strcpy_s(name, n); }
	friend istream& operator >> (istream&, CTool&);
	friend ostream& operator << (ostream&, const CTool&);
	int getRecord() { return record; }
	double getNum();
	void setTool(int r) 
	{
		cin.ignore(30,'\n');
		record = r;
		cout << "\nEnter tool name:  ";
		cin.getline(name,30);
		cout <<"\nEnter quantity:  ";
		quantity = static_cast<int>(getNum());
		cout <<"\nEnter cost:  ";
		cost = getNum();
	}	
};

int fileMenu();
int inventoryMenu();
bool getValidRecord(int&);
bool isYes();
double getNum();
void handleNewRecord(fstream&, CTool, int);
void handleUpdateRecord(fstream&, CTool, int);
void handleDeleteRecord(fstream&, CTool, int);
void printRecord(fstream&, CTool, int);
void initializeSUD(fstream& f, CTool& t);


bluecoder,

How do I make it global?
Don't declare getNum() inside the class definition. Instead declare the function in the global namespace before the class definition.
Ok it works if I declare it in the global namespace. Is that a safe or generally accepted practice though?
Sure is, you could always wrap it in a namespace that makes sense...

1
2
3
4
namespace Helpers
{
    double getNum();
}


Then when you use it...
Helpers::getNum();
Thanks for the help. I created a Utility namespace. Now my code is clean. =)
Topic archived. No new replies allowed.