У меня проблемы с вызовом функции шаблона (removeStu ()). При тестировании, не объявляя его как шаблонную функцию, он работает частично, но не в той степени, в которой я этого хочу. Вот сообщение об ошибке:
error: ‘removeStu’ was not declared in this scope
removeStu(stuAr, count, key);
У меня нет removeStu, объявленного прототипом, и когда я это получаю, я получаю следующие ошибки:
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);
Вот мой главный раздел, где я вызываю 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;
}
Вот функция 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 (), которая вызывается методом 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;
}
}
И функция find () вызывается функцией 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;
}