|
-
Mar 13th, 2007, 06:19 AM
#1
Thread Starter
Hyperactive Member
Help with template function
have a problem, I want to create a function that can handle any type and react to it accordingly. So I have used a template on my function, the problem is that I am trying to tell my function that I can only fill a string with a character array if the type of the varible is a string.
Despite this it still churns out the old "error C2440: '=' : cannot convert from 'char [100]' to 'int'" message.
My code is such:
Code:
//// ********* EditField: Displays the old value of a field and allows the user to modify a fields data.
template <class T>
void EditField(T value)
{
char buffme[100];
cout << "Old: " << value << endl;
cout << "Enter new:->";
cin.getline(buffme,99);
if(typeid(value).name() == typeid(int).name()) {value = atoi(buffme); return;}
if(typeid(value).name() == typeid(string).name()) {value = buffme; return;}
if(typeid(value).name() == typeid(float).name()) {value = atof(buffme); return;}
}
Can anyone help?
-
Mar 13th, 2007, 07:01 AM
#2
Re: Help with template function
In a template function all code is compiled for each type. So each of the branches of the if statement is compiled, even if they are never executed. The solution in this case is to handle the input using an overloaded function:
Code:
parse(buffer, value);
// Interpret a string as a value of type T
void parse(const string& buffer, string& out) { out = buffer; }
void parse(const string& buffer, int& out) { out = atoi(buffer); }
//etc.
You could also use "cin >> value" directly, this function is overloaded for most common types.
-
Mar 13th, 2007, 09:19 AM
#3
Thread Starter
Hyperactive Member
Re: Help with template function
Thank you, it is as I feared. I would have to overload 3 types and repeat my code for each of them.
-
Mar 13th, 2007, 09:24 AM
#4
Re: Help with template function
Not at all, you only have to 'repeat' the bit that is different. You can call overloaded functions, and even other template functions, from a template function:
Code:
template <class T>
void EditField(T& value) {
cout << "Old: " << value << endl;
cout << "Enter new:->";
read(value);
}
template <typename T>
void read(T& value) {
cin >> value; // handles integers and floats
}
void read(string& value) {
getline(cin, value);
}
-
Mar 13th, 2007, 01:55 PM
#5
Thread Starter
Hyperactive Member
Re: Help with template function
Ah good thanks again, I am starting to regret using the string class in my program . I should have used a 2D character array:
-
Mar 13th, 2007, 03:53 PM
#6
Re: Help with template function
What's that got to do with anything?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|