I know there are several answers similar to this question already but I think my lack of experience in C++ is preventing me from understanding them fully.
Problem: I have an assignment where I need the user to enter a value which should be of type 'double'. I then need to pass that user input into a custom method for a class I created and have the method validate that this input is indeed a 'double'.
I saw several posts where people validated the data type right after it was entered using a while
loop and the cin
operator. This doesn't help me because I need to validate the data type in the method.
Here is what I have so far:
Main.cpp:
...
int main() {
string name;
double gpa; // value that should be validated
cout << Enter GPA: ";
cin >> gpa;
myClass student_1;
student_1.validateGPA(gpa);
return 0;
}
myClass.h
...
template <class validation>
void validateGPA(validation);
myClass.cpp
...
template <class validation>
string myClass::validateGPA(validation gpa) {
return typeid(newGPA).name();
}
As written currently I am receiving this Error:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall myClass::validateGPA<double>(double)" (??$validateGPA@N@myClass@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@N@Z) referenced in function _main GPA_Assignment C:\Users\josep\source\repos\ccv\GPA\GPA\GPA_Assignment.obj 1
Without templating the function, the program ran but no matter what I entered at the prompt, the validation method always returned 'double'. I am assuming that is bc of how I declared the gpa varaible in the beginning, but I don't know a better way.
Thoughts!??