How to declare an object<template> in c++ - c++

I'm having the hardest time figuring this thing out. The error is in line 58 of main.cpp, I wrote a noticeable comment on line 58.
|58|error: expected primary-expression before ';' token
Main.cpp
#include <iostream>
#include <vector>
#include "LinkedList.h"
using namespace std;
bool eic(const string &str1, const string &str2){
if(str1.length() != str2. length())
return false;
for(int i = 0; i < str1.length(); i++)
if(toupper(str1[i]) != toupper(str2[i])) return false;
return true;
}
vector<string> tokenizer(const string &str, char delim, bool emptyok)
{
vector<string> tokens;
string t;
for(int i = 0; i < str.length(); i++)
{
if (str[i] == delim)
{
if(emptyok || (t.length() != 0))
tokens.push_back(t);
t.clear();
continue;
}
t.push_back(str[i]);
}
if(emptyok || (t.length() != 0)) tokens.push_back(t);
return tokens;
}
int main(){
LinkedList<int> sList;// = LinkedList<int>;
string input;
cout << "Type 'commands' to see the list of commands" << endl;
cin >> input;
vector<string> inputV = tokenizer(input,' ',false);
while(!eic(input,"exit")){
if(eic(input,"commands")){
cout << endl;
cout << "Do not include <> in any commands" << endl;
cout << endl;
cout << "Create <name of list>: Create a new list and names it." << endl;
cout << "Print <name of list>: Prints out the entire list." << endl;
cout << "Add <name of list> <item>: Adds an element to the list." << endl;
cout << "Delete <name of list> <item>: Deletes an element from the list." << endl;
cout << "DeleteAll <name of list> <item>: Deletes all occurences of the element from the list." << endl;
cout << "MakeEmpty <name of list>: Removes all elements from the list." << endl;
cout << "Length <name of list>: Tells you how many elements are in the list" << endl;
cout << "Remove <name of list> deletes an entire list" << endl;
cout << "Exit: Terminates the program" << endl;
}
else if(eic(inputV[0],"create")){
sList = LinkedList<int>; // LINE 58 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sList.setName(inputV[1]);
cout << sList.getName();
//cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"print")){
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"add")){
//sList->insertItem(9);
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"delete")){
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"deleteAll")){
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"makeEmpty")){
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"length")){
cout << "This function is still under construction" << endl;
}
else if(eic(inputV[0],"remove")){
cout << "This function is still under construction" << endl;
}
else cout << endl << "Invalid inquiry, please enter 'commands' to see a list of valid commands." << endl;
cin >> input;
}
}
If you need it here is my LinkedList.cpp file
#include <iostream>
#include "LinkedList.h"
using namespace std;
template <class xtype>
LinkedList<xtype>::LinkedList()
{
cout << "List created successfuly\n";
}
template <class xtype>
void LinkedList<xtype>::setLength(int x){
length = x;
}
template <class xtype>
int LinkedList<xtype>::getLength(){
return length;
}
template <class xtype>
void LinkedList<xtype>::setName(string x){
name = x;
}
template <class xtype>
string LinkedList<xtype>::getName(){
return name;
}
template <class xtype>
void LinkedList<xtype>::insertItem(xtype item){
node<xtype> *temp = new node<xtype>;
if(head == NULL || head->info > item){
temp->next = head;
head = temp;
}
else{
node<xtype> *q = head;
node<xtype> *p = head->next;
while(p != head && p->info <= item){
q = p;
p = p->next;
}
q->next = temp;
temp->next = p;
}
}
template class LinkedList<int>;
And the LinkedList header file
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class xtype>
struct node{
xtype info;
node *next;
node *prev;
};
template <class xtype>
class LinkedList
{
public:
LinkedList();
int getLength();
void setLength(int);
void setName(string);
string getName();
//bool searchItem(xtype item);
void insertItem(xtype item);
//void deleteItem(xtype item);
//int numOccur(xtype item);
protected:
private:
node<xtype> *head;
node<xtype> *term;
int length;
string name;
};
#endif // LINKEDLIST_H
Any help you can give me would be much appreciated. I'm brand new to c++, coming from java, and I've just been slaving over this all of last night up until now.

When you declare sList as LinkedList<int>, you already have invoked the default constructor to initialize sList. There's no need to assign this explicitly, as you (try to) do in the failing line.
This demonstrates a confusing but vital concept when coming from Java to C++ : RAII

Related

AVLTree can't find data when searched for

I am working on an assignment for school. I have to create an AVLTree, read data from a text file and then create Professor object with that data. However, when I try and search for a Professor by name, it can't be found. It keeps defaulting to Michael Scott in the program and can't find who I'm looking for. My readFromFile function works perfectly and gets me all the data from the file, so I have the data. At this point it's a matter of issues with the AVL Tree. I'll link everything below.
Driver.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "AVLTree.h"
using namespace std;
void readFromFile(AVLTree *treeAccess) {
int numOfProfs = 0;
string line;
const char *path="/Users/collinchappell/Desktop/Program4Official/Program4Official/ProfessorData.txt";
fstream myFile (path, ios::in);
if (!myFile) {
cout << "Sorry can't read ProfessorData.txt!" << endl;
}
else {
// Splitting up all the data in the .txt file and the looping through using string stream
// Not the best solution, however nothing else would work for me so this is what I went with.
while (getline(myFile, line)) {
string n;
string nT;
string s;
int b1,b2,b3,b4,b5,b6,b7;
replace(line.begin(), line.end(), '$', ' ');
stringstream ss(line);
for (int k = 0; k < 11; k++) {
ss >> n;
ss >> nT;
n = n + " " + nT;
ss >> s;
ss >> b1;
ss >> b2;
ss >> b3;
ss >> b4;
ss >> b5;
ss >> b6;
ss >> b7;
numOfProfs++;
Professor p(n, s, b1, b2, b3, b4, b5, b6, b7);
cout << p;
treeAccess->insertNode(n, &p);
}
}
}
cout << numOfProfs << " Proffesors have added from the file." << endl << endl;
}
int main() {
AVLTree tree;
readFromFile(&tree);
bool cont = true;
while (cont) {
cout << "Which Computer Science professor do you want details about?" << endl;
tree.displayInOrder();
cout << endl << endl << "Type Professor Name: ";
string typeName;
getline(cin, typeName);
while (tree.searchNode(typeName) == NULL) {
cout << "Couldn't find that professor! Please type another: ";
getline(cin, typeName);
}
cout << tree.searchNode(typeName);
cout << "Would you like to search another? (Y/N): ";
string ans;
cin >> ans;
if (ans == "y" || ans == "Y") {
continue;
}
else if (ans == "n" || ans == "N") {
return 0;
}
}
return 0;
}
AVLTree.h:
#ifndef AVLTree_h
#define AVLTree_h
#include <iostream>
#include "Professor.h"
using namespace std;
class AVLTree {
private:
// TreeNode structure
struct TreeNode {
string name;
Professor *prof;
struct TreeNode* left;
struct TreeNode* right;
};
// Root of tree
TreeNode* root;
// Function declarations
void insert(TreeNode *&nodePtr, TreeNode *&newNode);
void destroySubTree(TreeNode *nodePtr);
void displayInOrder(TreeNode *nodePtr) const;
int height(TreeNode *nodePtr);
int diff(TreeNode *nodePtr);
void balance(TreeNode *&temp);
TreeNode* l_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
TreeNode* r_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
TreeNode* lr_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = (l_rotation(temp));
return r_rotation(parent);
}
TreeNode* rl_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = (r_rotation(temp));
return l_rotation(parent);
}
public:
AVLTree() { // Constructor
root = NULL;
}
~AVLTree() { // Destructor
destroySubTree(root);
}
void insertNode(string n, Professor* professor);
Professor* searchNode(string);
// Call private displayInOrder()
void displayInOrder() const {
displayInOrder(root);
}
};
#endif /* AVLTree_h */
AVLTree.cpp:
#include <iostream>
#include "AVLTree.h"
using namespace std;
// Insert a node into the tree recursively
void AVLTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) {
if (nodePtr == NULL) {
nodePtr = newNode;
}
else if (newNode->name < nodePtr->name) {
insert(nodePtr->left, newNode);
balance(nodePtr);
}
else {
insert(nodePtr->right, newNode);
balance(nodePtr);
}
}
// Destory the sub tree
void AVLTree::destroySubTree(TreeNode *nodePtr) {
if (nodePtr) {
if (nodePtr->left) {
destroySubTree(nodePtr->left);
}
if (nodePtr->right) {
destroySubTree(nodePtr->right);
}
delete nodePtr;
}
}
// Display the tree in order
void AVLTree::displayInOrder(TreeNode *nodePtr) const {
if (nodePtr) {
displayInOrder(nodePtr->left);
cout << nodePtr->name << endl;
displayInOrder(nodePtr->right);
}
}
// Get the tree's height
int AVLTree::height(TreeNode *temp) {
int leftHeight, rightHeight;
if (temp) {
leftHeight = height(temp->left);
rightHeight = height(temp->right);
if (leftHeight > rightHeight) {
return leftHeight + 1;
}
else if (leftHeight < rightHeight) {
return rightHeight + 1;
}
else {
return 0;
}
}
return 0;
}
// Get the difference
int AVLTree::diff(TreeNode *temp) {
int l_height = height(temp->left);
int r_height = height(temp->right);
return l_height - r_height;
}
// Balance the tree
void AVLTree::balance(TreeNode *&temp) {
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff(temp->left) > 0) // 2, 1 RIGHT
{
temp = r_rotation(temp);
cout << "\nRIGHT rotation";
}
else // 2, -1 LEFT-RIGHT
{
temp = lr_rotation(temp);
cout << "\nLEFT-RIGHT rotation";
}
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0) // -2, 1 RIGHT-LEFT
{
temp = rl_rotation(temp);
cout << "\nRIGHT-LEFT rotation";
}
else // -2, -1 LEFT
{
temp = l_rotation(temp);
cout << "\nLEFT Rotation";
}
}
}
// Insert a node, calls the private insert function
void AVLTree::insertNode(string n, Professor *proffesor) {
TreeNode *newNode = NULL; // Pointer to a new node.
newNode = new TreeNode;
newNode->name = n;
newNode->prof = proffesor;
newNode->left = newNode->right = NULL;
// Insert the node.
insert(root, newNode);
}
Professor* AVLTree::searchNode(string s) {
cout << "Name inputted into search: " << s << endl;
cout << endl << "Actual name: " << root->name << endl;
TreeNode *nodePtr = root;
while (nodePtr != NULL) {
if (nodePtr->name == s) {
return nodePtr->prof;
}
else if (nodePtr->name > s) {
nodePtr = nodePtr->left;
cout << "Left -> " << nodePtr << endl;
}
else {
nodePtr = nodePtr->right;
cout << "Right -> " << nodePtr << endl;
}
}
return NULL;
}
Professor.h:
#ifndef PROFESSOR_H
#define PROFESSOR_H
#include <iostream>
#include <iomanip>
using namespace std;
class Professor
{
private:
string name;
string course;
bool clearGrading;
bool goodFeedback;
bool caring;
bool reachable;
bool toughGrader;
bool lectureHeavy;
bool attendance;
public:
Professor(string n, string course, bool cG, bool gF, bool c, bool r, bool tG, bool lH, bool a) {
this->name = n;
this->course = course;
this->clearGrading = cG;
this->goodFeedback = gF;
this->caring = c;
this->reachable = r;
this->toughGrader = tG;
this->lectureHeavy = lH;
this->attendance = a;
}
friend ostream &operator << (ostream &strm, Professor &p)
{
strm << endl << endl;
strm << setw(30) << "Professor:" << setw(20) << p.name << endl;
strm << setw(30) << "Course:" << setw(20) << p.course << endl;
strm << setw(30) << "Clear Grading Criteria:" << setw(20);
if(p.clearGrading == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Provides Good Feedback:" << setw(20);
if(p.goodFeedback == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Caring:" << setw(20);
if(p.caring == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Reachable Outside of Class:" << setw(20);
if(p.reachable == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Tough Grader:" << setw(20);
if(p.toughGrader == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Lecture Heavy:" << setw(20);
if(p.lectureHeavy == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Attendance Mandatory:" << setw(20);
if(p.attendance == 0) strm << "no"; else strm << "yes";
strm << endl;
return strm;
}
};
#endif
ProfessorData.txt:
Michael Scott$CSC1310$0$0$1$1$0$1$1$Jim Halpert$CSC1310$1$1$1$0$0$0$0$Pam Beesly$CSC1300$1$1$1$0$1$0$1$Dwight Schrute$CSC2400$1$1$0$0$1$1$1$Angela Martin$CSC1300$1$0$0$0$1$1$1$Kelly Kapoor$CSC1300$0$0$0$0$0$0$0$Andy Bernard$CSC2400$1$0$1$1$0$1$1$Kevin Malone$CSC1310$0$1$1$1$0$0$0$Meredith Palmer$CSC1310$0$1$1$1$0$0$0$Phyllis Vance$CSC1300$1$1$1$0$1$0$0$Oscar Nunez$2400$1$1$1$1$1$1$1$
Running the program:
11 Proffesors have added from the file.
Which Computer Science professor do you want details about?
Andy Bernard
Angela Martin
Dwight Schrute
Jim Halpert
Kelly Kapoor
Kevin Malone
Meredith Palmer
Michael Scott
Oscar Nunez
Pam Beesly
Phyllis Vance
Type Professor Name: Andy Bernard
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
0x7ffeefbfee18Would you like to search another? (Y/N):
As you can see, I've put in some prints to kind of debug what's happening. I believe my objects are not getting inserted into the tree right, or my search function is off. Also, I'm not 100% on the balance function. Any help would be amazing.

"Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++

hi guys this is a uni project and at the moment have encountered this issue which comes up during compilation
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: LinkedListDemo.o:LinkedListDemo.cpp:(.text+0x178): undefined reference to std::operator<<(std::ostream&, std::LinkedList const&)' LinkedListDemo.o:LinkedListDemo.cpp:(.text+0x178): relocation truncated to fit: R_X86_64_PC32 against undefined symbol std::operator<<(std::ostream&, std::LinkedList const&)'
collect2: error: ld returned 1 exit status
make: *** [makefile:11: a1] Error 1
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
using namespace std;
void initialize(LinkedList &l1, LinkedList &l2)
{
l1.add("the black cat was sitting on the black mat that was on the black floor");
l2.add("the dog scared the cat and the cat ran away");
}
int main()
{
LinkedList firstList;
LinkedList secondList;
initialize(firstList, secondList);
cout << "Start lists:" << endl;
cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Concatenating the two lists onto list '1':" << endl;
firstList += secondList;
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'was' from list '1':" << endl;
firstList.remove("was");
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'away' from list '2':" << endl;
secondList.remove("away");
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'cat' from both lists:" << endl;
firstList.remove("cat");
secondList.remove("cat");
//cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Number of occurrences of 'black' in list 1: ";
cout << firstList.count("black") << endl << endl;
// Uncomment this section if you are implementing the extended version of the method remove()
// cout << "Removing 'on the black' from both lists:" << endl;
// firstList.remove("on the black");
// secondList.remove("on the black");
// cout << "List 1: " << firstList << endl;
// cout << "List 2: " << secondList << endl << endl;
cout << "Sorting list 1:" << endl;
firstList.sort();
//cout << firstList << endl << endl;
cout << "The program has finished." << endl;
return 0;
}
that is the main file which is meant to work and not be changed
#ifndef will_PC
#define will_PC
#include <cstdlib>
#include <string>
#include "node.h"
namespace std{
class LinkedList{
public:
node* get_Head() const;
void add(string input);
void remove(string Input);
void sort();
int count(string Input);
string getText();
void operator += (const LinkedList& list);
private:
node* head;
node* tail;
node* n;
};
//this is what outputs the object
std::ostream& operator << ( std::ostream &out, LinkedList const& lst);
}
#endif
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
using namespace std;
void LinkedList::add(string Input){
tail = NULL;
int count =0;
string word = "";
int len=Input.length();
for (int i=0; i< len;i++)
{
if (Input[i]==' ')
{
n = new node;
n->set_Data(word);
n->set_Previous(tail);
n->set_Next(NULL);
if(tail !=NULL){
tail->set_Next(n);
}
tail = n;
if (count ==0)
{
head = n;
}
word = "";
count +=1;
} else
{
word +=Input[i];
}
}
n = new node;
n->set_Data(word);
n->set_Previous(tail);
n->set_Next(NULL);
tail = n;
}
void LinkedList::remove(string Input){
node* temp;
node* hold;
temp =head;
while (temp!=NULL){
if(Input.compare(temp->get_Data())==0){
hold = temp->get_Next();
temp=temp->get_Previous();
temp->set_Next(hold);
hold->set_Previous(temp);
temp=temp->get_Next();
temp=temp->get_Next();
} else{
temp = temp->get_Next();
}
}
}
int LinkedList::count(string Input){
node* temp;
temp =head;
int count= 0 ;
while (temp != NULL){
if(Input.compare(temp->get_Data())==0){
count +=1;
}
temp = temp->get_Next();
}
return count;
}
void LinkedList::operator += (const LinkedList& list){
node* temp;
cout << list.get_Head()->get_Next()->get_Data()<<endl;
temp = list.get_Head();
while(temp!=NULL){
n=new node;
n->set_Data(temp->get_Data());
n->set_Previous(tail);
n->set_Next(NULL);
tail->set_Next(n);
tail= n;
temp=temp->get_Next();
}
}
void LinkedList::sort(){
node* temp;
temp =head;
while(temp!=NULL){
cout<<temp->get_Data()<<" ";
temp = temp->get_Next();
}
cout<<endl;
}
node* LinkedList::get_Head() const {
return head;
}
std::ostream& operator << ( std::ostream &out, LinkedList const& lst)
{
node* temp = lst.get_Head();
while(temp != NULL){
out<< " "<< temp->get_Data() <<" ";
temp = temp->get_Next();
}
return out;
}
#include <cstdlib>
#include <string>
#include "node.h"
using namespace std;
void node::set_Next(node* nextLink){
next = nextLink;
}
void node::set_Previous(node* previousLink){
previous = previousLink;
}
node* node::get_Next(){
return next;
}
node* node::get_Previous(){
return previous;
}
void node::set_Data(string input){
text = input;
}
string node::get_Data(){
return text;
}
#ifndef Will_Node
#define Will_Node
#include <cstdlib>
#include <string>
namespace std{
class node{
public:
void set_Next(node* nextLink);
void set_Previous(node* previousLink);
void set_Data(string input);
node* get_Next();
node* get_Previous();
string get_Data();
private:
string text;
node* next;
node* previous;
};
}
#endif
any help is greatly appreciated ive spent way too many hours cursing to try and fix this
You declare your operator << in namespace std (which is illegal, as noted in comments), but you define it in global namespace.
using namespace works with definitions of class methods, because compiler knows that there is a class LinkedList in namespace std, so it can connect it:
If add() is a member of LinkedList, and LinkedList is a member of namespace std, then the fully qualified name must be ::std::LinkedList::add()
But the operator is a free function, so the compiler has nothing to relate it to the previous declaration and it is placed in global namespace.
Solution:
Change your namespace to something different than std
Instead of adding using namespace in cpp files, wrap the whole content in namespace{} :
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
namespace X {
void LinkedList::add(string Input){
// all of the member definitions...
std::ostream& operator << ( std::ostream &out, LinkedList const& lst)
{
node* temp = lst.get_Head();
while(temp != NULL){
out<< " "<< temp->get_Data() <<" ";
temp = temp->get_Next();
}
return out;
}
} // namespace X
//file ends here
You could also wrap only your operator definition in namespace {}, but it's easier to avoid such issues if you by default put whole content in namespace.

Doubly Linked List c++ implementing with a Class

I am trying to create a doubly linked list:
class DblLinkedBag
{
private:
struct node{
string data;
node* next;
node* prev;
}*start=NULL;
int itemCount;
string item;
node *head;
node *tail;
public:
DblLinkedBag();
~DblLinkedBag();
int getCurrentSize();
bool isEmpty();
string add(string value);
bool remove(string item);
void clear();
bool contains(string target);
int getFrequencyOf();
string toVector();
string getItem();
void display();
};
So far, I have gotten add, isEmpty, getCurrentSize and clear to work. Now I just need the rest to work and I am having a hard time. My professor gave us a requirement that the class had to be implemented like this:
#include <iostream>
#include <string>
#include "LinkedBag.h"
using namespace std;
void displayBag(LinkedBag<string>& bag)
{
cout << "The bag contains " << bag.getCurrentSize()
<< " items:" << endl;
vector<string> bagItems = bag.toVector();
int numberOfEntries = (int) bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void copyConstructorTester()
{
LinkedBag<string> bag;
string items[] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
bool success = bag.add(items[i]);
if (!success)
cout << "Failed to add " << items[i] << " to the bag." << endl;
}
displayBag(bag);
LinkedBag<string> copyOfBag(bag);
cout << "Copy of bag: ";
displayBag(copyOfBag);
cout << "The copied bag: ";
displayBag(bag);
} // end copyConstructorTester
void bagTester()
{
LinkedBag<string> bag;
cout << "Testing the Link-Based Bag:" << endl;
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for
displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize()
<< "; should be 6" << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bag.add("extra") << endl;
cout << "contains(\"three\"): returns " << bag.contains("three")
<< "; should be 1 (true)" << endl;
cout << "contains(\"ten\"): returns " << bag.contains("ten")
<< "; should be 0 (false)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 2" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 1" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 0 (false)" << endl;
cout << endl;
displayBag(bag);
cout << "After clearing the bag, ";
bag.clear();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
} // end bagTester
int main()
{
copyConstructorTester();
bagTester();
return 0;
} // end main
So far this is what I have.
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class DblLinkedBag
{
private:
struct node{
string data;
node* next;
node* prev;
}*start=NULL;
int itemCount;
string item;
node *head;
node *tail;
public:
DblLinkedBag();
~DblLinkedBag();
int getCurrentSize();
bool isEmpty();
string add(string value);
bool remove(string item);
void clear();
bool contains(string target);
int getFrequencyOf();
string toVector();
string getItem();
void display();
};
DblLinkedBag::DblLinkedBag()
{
itemCount=0;
item;
}
string DblLinkedBag::add(string value)
{
node* n;
cout<<itemCount<<endl;
if(itemCount==0)
{
n=new node;
n->data=value;
n->prev=NULL;
head=n;
tail=n;
}
if(itemCount>0 && itemCount<7)
{
n= new node;
n->data=value;
n->prev=tail;
tail->next=n;
tail=n;
}
itemCount++;
return value;
}
void DblLinkedBag::display()
{
struct node* temp=start;
while(temp != NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int DblLinkedBag::getCurrentSize()
{
return itemCount;
}
bool DblLinkedBag::contains(string target)
{
//need help here, supposed to tell if the linked list contains a certain //string
bool found= false;
node* curPtr=start;
int i=0;
while (!found && (curPtr!=NULL)&& (i<itemCount))
{
if(target==curPtr->data)
{
found=true;
}
else
{
i++;
curPtr=curPtr->next;
}
}
return found;
}
bool DblLinkedBag::isEmpty()
{
bool empty;
if (itemCount==0)
{
empty=true;
}
else
empty=false;
return empty;
}
void DblLinkedBag::clear()
{
node* nodeToDelete=start;
while (start != NULL)
{
start=start->next;
nodeToDelete->next=NULL;
delete nodeToDelete;
}
itemCount=0;
}
bool DblLinkedBag::remove(string item)
{
//need help here
}
string DblLinkedBag::toVector()
{
//need help here this is supposed to send the linked list to a vector
vector<string> vct;
node* curPtr= start;
int counter = 0;
while ((curPtr != NULL) && (counter < itemCount))
{
vct.push_back(curPtr->data);
curPtr = curPtr->next;
counter++;
}
}
int DblLinkedBag::getFrequency()
{//supposed to show how many of a certain item are in the linked list
DblLinkedBag::~DblLinkedBag()
{
}
Any help implementing these class functions to create the other functions my professor gave me would be appreciated, I have tried all different kinds of implementations and I cannot seem to figure it out.
First: your method DblLinkedBag::clear has a error, nodeToDelete never change (just deletes first node)
bool DblLinkedBag::remove(string item)
{
node* curPtr=start;
while (curPtr!=NULL)
{
if(item==curPtr->data)
{
if(curPtr->prev) curPtr->prev->next = curPtr->next;
if(curPtr->next) curPtr->next->prev = curPtr->prev;
delete curPtr;
itemCount--;
return true;
}
else
{
curPtr=curPtr->next;
}
}
return false;
}
What do you expect getFrequency() to do?

Using Own Stack Class in Encoding Program - Determine Size and Top

I wrote a encoding program using the C++ stack library. Now I am trying to implement my own stack class, however I notice there are size() and top() member functions in the stack library. I am not sure how to implement my code without these functions, or how to write those functions in my class to get them to work properly with the code I already have.
Here is the areas that the stack library functions are being called in my readFileEncode(string filename, stack<char> &text, string cypher) function:
ifstream file(fileName, ios::in | ios::binary);
stack<char> temp;
char ch;
while (file.get(ch)){
temp.push(ch ^ cypher[temp.size() % cypher.length()]);
}
while (!temp.isEmpty()){
text.push(temp.top());
temp.pop();
}
Here is my stack class:
#include<iostream>
#define TRUE 1
#define FALSE 0
template <class TYPE>
class stack{
struct node{
TYPE element;
node *next;
};
public:
node *top;
int stackSize;
stack(void); //constructor
~stack(void); //destructor free the stack
void push(TYPE & value);
TYPE pop(void);
TYPE peek(void);
int isEmpty(void); //returns TRUE if empty
void print(void);
void reset(void); //pop all the elements off the stack
size_t size(void) const;
TYPE topOf(void) const;
};
template <class TYPE>
stack<TYPE>::stack(void){
top = NULL;
stackSize = 0;
}
template <class TYPE>
stack<TYPE>::~stack(void){
cout << "Entering Stack Destructor" << endl;
reset();
cout << "Exiting Stack Destructor" << endl;
}
template <class TYPE>
void stack<TYPE>::push(TYPE & value){
node *temp = new node;
if (temp == NULL){
cout << "Push: Memory Allocation Error" << endl;
exit(1);
}
temp->element = value;
temp->next = top;
top = temp;
stackSize++;
}
template <class TYPE>
TYPE stack<TYPE>::pop(void){
TYPE returnElement;
if (top != NULL){
node *temp = top;
returnElement = top->element;
top = top->next;
delete temp; //delete the node
stackSize--;
}
return(returnElement);
}
template <class TYPE>
TYPE stack<TYPE>::peek(void){
TYPE returnElement;
if (top != NULL)
returnElement = top->element;
cout << "Peek: " << returnElement << endl;
return(returnElement);
}
template <class TYPE>
int stack<TYPE>::isEmpty(void){
if (stackSize == 0)
return(TRUE);
else
return(FALSE);
}
template <class TYPE>
void stack<TYPE>::reset(void){
cout << "Reset Stack" << endl;
while (isEmpty() != TRUE){
pop();
}
}
template <class TYPE>
void stack<TYPE>::print(void){
cout << "Inside Print Stack" << endl;
cout << "Stack size = " << stackSize << endl;
node * temp = top;
while (temp != NULL){
cout << " " << temp->element << endl;
temp = temp->next;
}
}
template <class TYPE>
size_t size(void) const{
return stackSize;
}
template <class TYPE>
TYPE stack<TYPE>::topOf(void) const{
return (*top).element;
}
This stack class is based on what I know of stack's. If there is anything wrong, it is because this is the first time I have written a stack class.
So basically, I am having problems either 1) writing the size() and top() functions, or 2) rewriting the while loops in my readFileEncode() function to use what I have. I had help properly writing the code to work with the stack library, but now trying to implement my own class is causing me problems. Any help would be appreciated.
EDIT 1: With the help of dyp, I changed the variable name of int size to int stackSize everywhere it appears in the class. Also, I change the function top() to topOf()
EDIT 2: Changed void push(TYPE &value) to void push(TYPE const& value) in all instances. Code is updated above. I used the following main() to test the class:
#include <iostream>
#include <cstdlib>
#include "stack.h"
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
cout << "empty: " << s.isEmpty() << endl;
s.pop();
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
//cout << "top element: " << s.top() << endl;
cout << "empty: " << s.isEmpty() << endl;
system ("pause");
}
Everything worked fine, however when I attached the stack.h file to my encoding program, I get the following errors:
At file.put(text.top()); I get "term does not evaluate to a function taking 0 arguments," and the same error at text.push(temp.top());. I get the error, but not exactly sure how to fix it.
I also had to move public: in my class above node *top; int stackSize; because I got errors about them being private. This was not a problem with my test program. Not sure if that is ok either.
I used the following code to test my stack:
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
cout << "empty: " << s.isEmpty() << endl;
s.pop();
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
//cout << "top element: " << s.top() << endl;
cout << "empty: " << s.isEmpty() << endl;
system ("pause");
}
I changed the code in my following stack class to the following and it seems to have worked:
void push(TYPE const& value);
size_t size(void) const;
TYPE topOf(void) const;
and:
void stack<TYPE>::push(TYPE const& value){
node *temp = new node;
if (temp == NULL){
cout << "Push: Memory Allocation Error" << endl;
exit(1);
}
temp->element = value;
temp->next = top;
top = temp;
stackSize++;
}
template <class TYPE>
size_t stack<TYPE>::size(void) const{
return stackSize;
}
template <class TYPE>
TYPE stack<TYPE>::topOf(void) const{
return (*top).element;
}
Also changed all occurrences of top() in my main to topOf(). Not sure if this is all proper, but it works, and I will take dyp's advice and turn it in at CodeReview.

undefined reference to (function) c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined Reference to
I've been banging my head against this one set of error messages for about 4 hours now and I can't seem to figure it out. I've not posted here before, so I apologize in advance if it's not in the right area or I've done something wrong. At any rate, the error messages I'm receiving are:
main.cpp|28|undefined reference to `LinkedSortedList<Employee>::LinkedSortedList()'|
main.cpp|52|undefined reference to `empPrint(LinkedSortedList<Employee>&)'|
main.cpp|58|undefined reference to `empSave(LinkedSortedList<Employee>&, std::string)'|
main.cpp|65|undefined reference to `empLoad(LinkedSortedList<Employee>&, std::string)'|
main.cpp|70|undefined reference to `LinkedSortedList<Employee>::~LinkedSortedList()'|
main.cpp|70|undefined reference to `LinkedSortedList<Employee>::~LinkedSortedList()'|
obj\Debug\main.o||In function `Z9empSearchR16LinkedSortedListI8EmployeeE':|
main.cpp|109|undefined reference to `LinkedSortedList<Employee>::getHead()'|
My main.cpp is as follows:
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include "SortedList.h"
#include "LinkedSortedList.h"
#include "Employee.h"
#include "LinkedNode.h"
using namespace std;
void newEmp(LinkedSortedList <Employee>& empList);
void empSearch(LinkedSortedList <Employee>& empList);
void empPrint(LinkedSortedList <Employee>& empList);
void empSave(LinkedSortedList <Employee>& empList, string file);
void empLoad(LinkedSortedList <Employee>& empList, string file);
int main()
{
//int empID;
bool menuFinish = false;
LinkedSortedList<Employee> empList;
char selection;
while (!menuFinish)
//simple menu system through cout, the selection is read in through cin
//and converted to upper case for simplicity during the conditionals
cout << "Menu" << endl;
cout << "(I)nsert new record" << endl;
cout << "(E)mployee ID search" << endl;
cout << "(P)rint employee info" << endl;
cout << "(S)ave database to a file" << endl;
cout << "(L)oad database from file" << endl;
cout << "(Q)uit" << endl;
cout << "Enter selection " << endl;
cin >> selection;
selection = toupper(selection);
//menu selections are compared with their functions
if (selection == 'I')
newEmp(empList);
else if (selection == 'E')
empSearch(empList);
else if (selection == 'P')
empPrint(empList);
else if (selection == 'S')
{
string fileName;
cout << "Enter a filename to save the database to " << endl;
cin >> fileName;
empSave(empList, fileName);
}
else if (selection == 'L')
{
string fileName;
cout << "Enter the filename to load the database from " << endl;
cin >> fileName;
empLoad(empList, fileName);
}
else if (selection == 'Q')
menuFinish = true;
else
cout << "Incorrect choice " << endl;
}
//function creates a new employee
void newEmp(LinkedSortedList <Employee>& empList)
{
string firstName;
string lastName;
int empID = -1;
cout << "Please enter the first name " << endl;
cin >> firstName;
cout << "Please enter the last name " << endl;
cin >> lastName;
while (empID > 9999999 || empID < 0)
{
cout <<"Please enter the employee ID " << endl;
cin >> empID;
}
//puts the employee in the db unless they're already found, then outputs an
//error on the screen
Employee emp(firstName, lastName, empID);
bool findEmp = empList.find(emp);
if (!findEmp)
empList.insert(emp);
else
cout << "Emlpoyee ID " << empID << " already in use " << endl;
}
//function to search for an employee based on their employee ID
void empSearch (LinkedSortedList <Employee>& empList)
{
int empID;
int sizeOfList = 0;
bool noEmp = true;
cout << "Enter employee ID " << endl;
cin >> empID;
LinkedNode <Employee>* temp = empList.getHead();
while (sizeOfList < empList.size() && noEmp)
{
sizeOfList++;
if (empID == temp->value.getEmpID())
{
cout << "Searched " << sizeOfList << "employees " << endl;
cout << "Found record: " << temp->value;
noEmp = false;
}
temp = temp->next;
}
if (noEmp)
cout << "Search of " << sizeOfList << " employees. Employee not found" << endl;
}
//function used to print the first and last five employees from the db
void empPrint (LinkedSortedList <Employee>& empList)
{
if (empList.size() <= 10)
{
empList.print();
}
else
{
LinkedNode<Employee>* temp = empList.getHead();
cout << "First five employees: " << endl;
for (int i = 0; i < 5; i++)
{
cout << temp->value << endl;
i++;
temp = temp->next;
}
int midList = empList.size()-5;
for (int i = 0; i < midList; i++)
{
temp = temp->next;
}
cout << "Last five employees: " << endl;
for (int i = 0; i < 5; i++)
{
cout << temp->value << endl;
i++;
temp = temp->next;
}
}
}
//function used to save the employee information from the db to a file
void empSave(LinkedSortedList<Employee>& empList, string fileName)
{
string lastName;
string firstName;
//int empID;
ofstream output;
output.open(fileName.c_str());
if (!output)
{
cout << "File not saved" << endl;
}
else
{
LinkedNode<Employee>* temp = empList.getHead();
int i = 0;
while (i < empList.size())
{
output << temp->value.getLastName() << " " << temp->value.getFirstName() << " " << temp->value.getEmpID() << endl;
i++;
temp = temp->next;
}
}
output.close();
}
//function used to load the employee information from a file to the db
void empLoad(LinkedSortedList<Employee>& empList, string fileName)
{
if (empList.size() > 0)
{
empList.clear();
}
ifstream input;
input.open(fileName.c_str());
if (!input)
{
cout << "No file exists" << endl;
}
else
{
int empID;
string firstName;
string lastName;
string delimiter;
while(input.good());
{
getline(input, delimiter, '\n');
getline(input, lastName, ' ');
getline(input, firstName, ' ');
input >> empID;
Employee emp(lastName, firstName, empID);
bool empFound = empList.find(emp);
if(!empFound)
{
empList.insert(emp);
}
else
cout << "Employee already exists" << endl;
}
}
}
My LinkedSortList.cpp is:
#ifndef _LinkedSortedList_
#define _LinkedSortedList_
#include "Employee.h"
#include "LinkedSortedList.h"
#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//default constructor
//sets head to null and makes the size of the list 0
template <class Elem>
LinkedSortedList<Elem>::LinkedSortedList()
{
head = NULL;
listSize = 0;
}
//destructor, clears list THEN deletes the head so memory leaks are
//stopped
template <class Elem>
LinkedSortedList<Elem>::~LinkedSortedList()
{
clear();
delete head;
}
//clears the list, freeing memory and stopping leaks and resets the
//list size
template <class Elem>
void LinkedSortedList<Elem>::clear()
{
LinkedNode<Elem> *indexPtr = head;
while (head != NULL)
{
head = head->next;
delete indexPtr;
indexPtr = head;
}
listSize = 0;
}
//finds a search value in the list... if it finds it then it returns true,
//otherwise it returns false
template <class Elem>
bool LinkedSortedList<Elem>::find(Elem searchValue) const
{
LinkedNode<Elem>* indexPtr = head;
while (indexPtr != NULL)
{
if (indexPtr->value == searchValue)
{
return true;
}
indexPtr = indexPtr->next;
}
return false;
}
//gets and DELETES first value in the list - if it finds nothing then
//return false, otherwise true
template <class Elem>
bool LinkedSortedList<Elem>::getFirst(Elem &returnValue)
{
LinkedNode<Elem>* indexPtr = head;
if (indexPtr == NULL)
return false;
else
{
head = head->next;
returnValue = indexPtr->value;
delete indexPtr;
listSize--;
return true;
}
returnValue = indexPtr->value;
}
//prints the list to cout or prints a warning if the list contains
//no values
template <class Elem>
void LinkedSortedList<Elem>::print() const
{
if (head == NULL)
{
cout << "No elements in the list" << endl;
}
else
{
LinkedNode<Elem>* indexPtr = head;
while (indexPtr != NULL)
{
cout << indexPtr->value << endl;
indexPtr = indexPtr->next;
}
}
}
//returns the size of the list to the caller
template <class Elem>
int LinkedSortedList<Elem>::size() const
{
return listSize;
}
//inserts a value into the list where it should go, if the list is empty it will
//say there are no existing nodes
template <class Elem>
bool LinkedSortedList<Elem>::insert(Elem newValue)
{
LinkedNode<Elem>* indexPtr = head;
LinkedNode<Elem>* newNode;
//newNode->value = newValue;
try
{
newNode = new LinkedNode<Elem>(newValue);
}
catch(exception e)
{
cout<<"Exception reached: " << e.what() << endl;
return false;
}
//checks to see if the list is empty, if it is then it makes the newNode
//the head
if (head == NULL)
{
cout << "No existing nodes" << endl;
head = newNode;
cout << "First node is now " << head->value << endl;
listSize++;
return true;
}
/*looks to see if the value of the newNode is less than or equal to the
index, if it is then it sets the point of the newNode equal to the head
then makes the head the newNode and increments the listSize by one to keep
track of the size of the list and returns true*/
else if (newNode->value <= head->value)
{
newNode->next = head;
head = newNode;
listSize++;
return true;
}
/*if the newNode value is greater than the index, then:*/
else
{
while(indexPtr->next != NULL && newNode->value > indexPtr->next->value)
{
indexPtr = indexPtr->next;
}
if (indexPtr->next == NULL)
{
indexPtr->next = newNode;
listSize++;
return true;
}
else
{
newNode->next = indexPtr->next;
indexPtr->next = newNode;
listSize++;
return true;
}
}
}
//added for project 2 to return the head of the LL
template <class Elem>
LinkedNode<Elem>* LinkedSortedList<Elem>::getHead()
{
return head;
}
#endif
The Employee.cpp is:
#ifndef _Employee_
#define _Employee_
#include "Employee.h"
#include "LinkedSortedList.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//blank default constructor
Employee::Employee()
{
}
//constructor that takes 3 parameters and sets them
Employee::Employee(string lastName, string firstName, int eID)
{
this->lastName = lastName;
this->firstName = firstName;
this->empID = eID;
}
//blank deconstructor
Employee::~Employee()
{
}
//overloaded equality operator
bool Employee::operator==(Employee &nextEmployee)
{
if (this->empID == nextEmployee.empID)
return true;
else
return false;
}
//overloaded less than or equal to operator
bool Employee::operator <= (Employee &nextEmployee)
{
if (this->empID <= nextEmployee.empID)
return true;
else
return false;
}
//overloaded greater than or equal to operator
bool Employee::operator >= (Employee &nextEmployee)
{
if (this->empID >= nextEmployee.empID)
return true;
else
return false;
}
//overloaded less than operator
bool Employee::operator < (Employee &nextEmployee)
{
if (this->empID < nextEmployee.empID)
return true;
else
return false;
}
//overloaded greater than operator
bool Employee::operator > (Employee &nextEmployee)
{
if (this->empID > nextEmployee.empID)
return true;
else
return false;
}
// overloaded output stream operator
ostream& operator<<(ostream& os, const Employee empl)
{
os << "Last: " << empl.lastName << endl;
os << "First: " << empl.firstName << endl;
os << "Employee ID: " << empl.empID << endl;
return os;
}
#endif
My header file, LinkedSortedList.h:
#ifndef _LinkedSortedListClass_
#define _LinkedSortedListClass_
#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
#include <fstream>
#include <string>
//using namespace std;
template <class Elem>
class LinkedSortedList : public SortedList< Elem >
{
public:
LinkedSortedList();
~LinkedSortedList();
virtual void clear();
virtual bool insert(Elem newValue);
virtual bool getFirst(Elem &returnValue);
virtual void print() const;
virtual bool find(Elem searchValue) const;
virtual int size() const;
LinkedNode<Elem>* getHead(); //added for project 2
private:
LinkedNode<Elem>* head;
int listSize;
};
#endif
And finally (WHEW!) my Employee.h is here:
#ifndef _EmployeeClass_
#define _EmployeeClass_
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Employee
{
public:
Employee();
~Employee();
Employee(string, string, int);
bool operator == (Employee& nextEmployee);
bool operator <= (Employee &nextEmployee);
bool operator >= (Employee &nextEmployee);
bool operator < (Employee &nextEmployee);
bool operator > (Employee &nextEmployee);
friend ostream& operator<<(ostream& os, const Employee empl);
string getLastName(){return lastName;}
string getFirstName(){return firstName;}
int getEmpID(){return empID;}
private:
string lastName;
string firstName;
int empID;
};
#endif
I'm just not seeing the problem here, I've spoken to my TA, the helproom staff and other students. I haven't heard back from the prof yet, but I doubt I'll hear from her this weekend anyway. Thank you VERY much for any insight you can provide. I need some tylenol.
-Josh
How are you compiling your code? If you are using an IDE such as Microsoft Visual C++, be sure to create a project and add all of your .cpp files (not your .h files) to it. If you are using the command-line, be sure to include all of your filenames as command-line arguments. If this still doesn't help. Please include a description of the steps you are using to compile the program.
Edit:
There are several issues with your code:
#ifndef ... #define... include guards are meant for files which will appear in #include directives in other files. Typically these are only .h files since it is strongly discouraged to #include .cpp files.
Classes which use templates must all be in one .h file, not separated into a .h and .cpp file. (READ: In case I haven't made this clear enough, your LinkedSortedList class must ALL be in one .h file.)
Your main.cpp file declares several functions which are not later defined. If you don't want to take the time to implement these functions yet, you need to at least add empty stubs for them.
Be sure to compile and link all of your source files using a project in your IDE or using the correct command-line arguments. This is what I described in my original answer above.
To get templates instantiated automatically the compiler needs to the see the definition at the point of use. If you don't want to make the template definition visible you need to use explicit instantiation. How to do either of these is frequently answered.
BTW, please don't use names reserved for the implementation of the C++ compiler and its standard library as include guards: names starting with an underscore followed by a capital letter are reserved in all contexts for the C++ implementation.
My LinkedSortList.cpp is ...
That's the source of most of your problems. Doing what you did doesn't make sense. You need to define those inline in your header file, LinkedSortedList.h. That covers most of your problems the others, where are you defining empPrint(LinkedSortedList<Employee>&)?
First of all I cant see definition of these three:-
void empPrint (LinkedSortedList <Employee>& empList);
void empSave (LinkedSortedList <Employee>& empList, string file);
void empLoad (LinkedSortedList <Employee>& empList, string file);
Hope implementing them will reduce the errors.
C++ template methods and functions are not turned into machine code until the compiler runs into a call to the function with a specific type. But that means that the code that defines a template method, for example all of LinkedSortedList.cpp, actually does not produce any code at all when the compiler sees it.
You need to move all of the code from LinkedSortedList.cpp into LinkedSortedList.h (and similarly for the other templated classes) so that the compiler can produce the code for these methods when they are called. For example the compiler will generate the code for LinkedSortedList::LinkedSortedList() when it is called in main.cpp, but only if it's already seen the definition for this method in a header file.
Your other option is to use 'explicit instantiation' to force particular versions of particular methods to be compiled into object code.