I am having issues calling a template function (removeStu()). When testing it without declaring it as a template function it works partly, but not to the extent I want it to. Here is the error message:
error: ‘removeStu’ was not declared in this scope
removeStu(stuAr, count, key);
I do not have removeStu declared as a prototype and when I do I get these errors:
college.cpp:23:16: error: variable or field ‘removeStu’ declared void
void removeStu(T1 stuAr[], int& count, const T2& key);
^
college.cpp:23:16: error: ‘T1’ was not declared in this scope
college.cpp:23:28: error: expected primary-expression before ‘int’
void removeStu(T1 stuAr[], int& count, const T2& key);
college.cpp:23:40: error: expected primary-expression before ‘const’
void removeStu(T1 stuAr[], int& count, const T2& key);
^
college.cpp: In function ‘int main()’:
college.cpp:44:30: error: ‘removeStu’ was not declared in this scope
removeStu(stuAr, count, key);
Here is my main where I call removeStu
int main()
{
int count = 0; //the number of students in the array
int curID = START; //curID is the student id used for the next student you creating
student stuAr[SIZE]; //student array with constant size of 100
cout << "====Student1====" << endl;
addStu(stuAr, count, curID); //adds student to array from input
cout << "====Student2====" << endl;
addStu(stuAr, count, curID); //adds student to array from input
cout << "====Student3====" << endl;
addStu(stuAr, count, curID); //adds student to array from input
allStuInfo(stuAr, count); //shows all info inside stuAr
int key;
cout << "Enter ID of student you wish to remove: ";
cin >> key;
removeStu(stuAr, count, key);
allStuInfo(stuAr, count); //shows all info inside stuAr
return 0;
}
Here is removeStu
template <class T1, class T2>
void removeStu(T1 ar[], int& n, const T2& id) //case 3 from admissions
{
string f, l;
if(remove(ar, n, id))
{
id--;
cout << f << " " << l << " has been removed." << endl;
}
else
cout << "student with id " << id << " doesn't exist" << endl;
}
remove() function which is called by removeStu()
template <class T1, class T2>
bool remove(T1 ar[], int n, const T2& key)
{
int temp = find(ar, n, key);
if(temp == -1)
{
return false;
}else
{
for (int temp; temp < n; ++temp)
{
ar[temp] = ar[temp + 1]; // copy next element left
}
return true;
}
}
And find() function being called by remove()
template <class T1, class T2>
int find(const T1 ar[], int n, const T2& key)
{
for(int i = 0; i < n; i++)
{
if(ar[i] == key)
{
return i;
}
}
return -1;
}