继续收到以下错误
“main.cpp :(。text + 0x1c0):未定义引用Student<int>::Add(int const&)'"
"main.cpp:(.text+0x1fa): undefined reference to
Student :: Remove()'”
“main.cpp :(。text + 0x21f):未定义引用`Student :: Clear()'”
也是“对'WinMain'的未定义引用
的main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "student.h"
using namespace std;
int main()
{ Student <int> stud1; // a student
char choice, answer; // handles input from menu and controls loop
double score; // the iten to be added to the end of the array
do{
system("CLS"); // clears the screen
cout <<setw(30)<< " Main Menu\n\n\n"; // menu of options to add/remove or clear
cout << setw(15)<< " "<< "(1)- (A)dd\n\n";
cout << setw(15)<< " "<< "(2)- (R)emove \n\n";
cout << setw(15)<< " "<< "(3)- (C)lear\n\n\n";
cout << setw(35)<< "Enter Choice: ";cin >> choice;
choice = toupper(choice);
switch (choice)
{ case '1':
case 'A':
cout << "\nEnter score: "; cin >> score;
if (stud1.Add(score)) // call to the add method
{cout << score << " Added\n\n";}
break;
case '2':
case 'R':
if(stud1.Remove()) // call to the remove method
{cout << "\n\nRemoved\n\n";}
break;
case '3':
case 'C':
if (stud1.Clear())
{cout << "\n\nCleared\n\n";} // call to the clear method
break;
}
cout << "Another Operation? "; cin >> answer; answer = toupper(answer);
} while (answer == 'Y');
cin.get();
return 0;
}
student.cpp
#include <iostream>
#include <iomanip>
#include "student.h"
using namespace std;
// Adds a score to the end of the array
template <class T>
bool Student<T>::Add(const T &s)
{
if (isFull()) return false;
score[count] = s;
count++;
return true;
}
// Removes the last score of the array
template <class T>
bool Student<T>::Remove()
{
if (isEmpty()) return false;
score[count] = 0;
return true;
}
// Clears the array of all contents
template <class T>
bool Student<T>::Clear()
{
if (isEmpty()) return false;
Print();
for (count != 0; count--;)
{
score [count] = 0;
}
count = 0;
return true;
}
// Checks if the array is full
template <class T>
const bool Student<T>::isFull()
{
if (count == 5)
{
cout << "The array is full" << endl;
return true;
}
return false;
}
// Checks if the array is empty
template <class T>
const bool Student<T>::isEmpty()
{
if (count == 0)
{
cout << "The array is empty" << endl;
return true;
}
return false;
}
// Prints the contents of the array from last to first
template <class T>
const void Student<T>::Print()
{
cout << "Name: " << name << endl;
for (int x = count; x != 0; x--)
{
cout << "Test score: " << score [x-1] << endl;
}
}
// Explicit instantiation of class template to avoid compiler errors
template class Student<double>;
template class Student<int>;
student.h
// Class declaration
#include <string>
#include <iostream>
using namespace std;
// Guard
#ifndef STUDENT_H
#define STUDENT_H
template <class T>
class Student{
private:
// Class data members
string name;
T score[5];
int count;
public:
// Constructor
Student(int c = 0)
{
cout << "Enter student name: ";
cin >> name;
count = c;
}
// Destructor
~Student(){}
// Member Functions
bool Add(const T &);
bool Remove();
bool Clear();
const bool isFull();
const bool isEmpty();
const void Print();
};
#endif
// Guard