这两个类都在头文件中,这不是问题。出于某种原因,我的程序告诉我有一个未定义的引用错误。我不完全确定为什么会这样。
我有 2 个类的一个实现文件。这可能是错误的来源吗?
这是我的第一堂课
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <string>
using namespace std;
// Node class
template <class T>
class Node
{
private:
int ID;
string name;
T test1, test2, test3;
static const int counter = 0;
Node* next;
public:
Node()
{
ID = NULL;
name = NULL;
test1 = NULL;
test2 = NULL;
test3 = NULL;
}
~Node(){}
void SetID(); //giving a value for employee number
void SetName(); //giving a value for yearly salary
void SetTests();
void SetNext(Node*);
};
#endif
这是我的第二节课
#include <iostream>
#include <string>
#ifndef LIST_H
#define LIST_H
#include "Node.h"
using namespace std;
template <class T>
class List
{
Node<T>* head; //creating the head
// head is an object that stores the address of the first node
string dash;
public:
// constructor that initializes every list to null
List()
{
dash.assign(80,'-'); // variable used for aesthetics
head = NULL;
}
~List(){}
// prtototype of the list member functions
void Add();
};
#endif
这是我的实现文件
#include <iostream>
#include <fstream>
#include "Node.h"
#include "List.h"
using namespace std;
template <class T>
void List<T> :: Add ()
{
// Create a new node
Node<T>* newNode = new Node<T>();
newNode->SetID(); //giving a value for employee number
newNode->SetName(); //giving a value for yearly salary
newNode->SetTests();
newNode->SetNext(NULL); //setting the next node equal to NULL
/*If(!isEmpty())
{*/
// Adding values to the elements of the head when head is empty
if(head == NULL)
head = newNode;
//Inserting Elements at the beginning of the list
else
{
newNode->SetNext(head); //make newNode -> point to the head
head = newNode; //make the head newNode
}
}
这是主文件
#include "Node.h"
#include "List.h"
#include <iostream>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
/* Main*/
////////////////////////////////////////////////////////////////////////////////
template <class T>
void menu(List<T> &);
int main()
{
// New list
List<int> list;
menu(list); //List function call
return 0;
}
///////////////////////////////////////////////////////////////////////////////
/*This function dispalys a Menu for the user to
pick the desired action to the linked list.
It uses the member functions of the class List, 2 loops
and a switch statement to execute these actions */
///////////////////////////////////////////////////////////////////////////////
template <class T>
void menu(List<T> &list)
{ char choice;
do{
system("CLS"); // use #include <cstdlib> to be able to use this
system("CLS") clears the window
cout << "\t\t\tWelcome User\n";
cout << "\t\t\tMain Menu\n\n";
cout << "\t(A)dd\n";
cout << "\t(L)ocation\n";
cout << "\t(R)emoves\n";
cout << "\t(C)lear\n";
cout << "\t(P)eek\n";
cout << "\t(A)verage\n";
cout << "\t(G)rade\n";
cout << "\t(E)xit\n";
cout << "\t\tWhat would you like me to do? ";
cin >>choice;
choice = toupper(choice); // chaging the character to capital
//validation of entered choice
while (choice != 'A' && choice != 'L' && choice != 'R' && choice != 'C'
&& choice != 'P' && choice != 'G' && choice != 'E' )
{
cout << "\n\t\tSorry invalid input\n\t\tPlease enter one of the ";
cout << "choices below : ";
cin >> choice;
}
cin.ignore();
while (choice != 'A' && choice != 'L' && choice != 'R' && choice != 'C'
&& choice != 'P' && choice != 'G' && choice != 'E' )
//switch statement used
switch (choice)
{ case 'A': //To insert
list.Add();
cout << endl;
cin.get();
cin.ignore();
break;}
}while (choice != 'E'); //Runing the program until E is entered
}
我不明白为什么我有一个参考错误,因为我确保我有警卫,并包含所有头文件。
是什么给了我未定义的参考错误?
(.text$_Z4menuIiEvR4ListIT_E[_Z4menuIiEvR4ListIT_E]+0x199): undefined reference to `List<int>::Add()'
C:\Users\Documents\collect2.exe [Error] ld returned 1 exit status
C:\Users\Documents\Makefile.win recipe for target '"Last' failed