R
Red
I am taking a c++ course. I have a simple program that just wont compile and
I cant seem to figure out why. If I compile the class file without
referencing it in the int main() it will compile but once I try to createon
abject of the class type I get:
Recursive error LNK2019: unresolved external symbol "public: __thiscall
UnsortedList<int>::~UnsortedList<int>(void)" (??1?$UnsortedList@H@@QAE@XZ)
referenced in function _main
Recursive error LNK2019: unresolved external symbol "public: __thiscall
UnsortedList<int>::UnsortedList<int>(void)" (??0?$UnsortedList@H@@QAE@XZ)
referenced in function _main
Recursive is the name of the project.
Please help.
Thanks
Header File
#pragma once
template <class ItemType>
struct NodeType;
template <class ItemType>
class UnsortedList
{
public:
UnsortedList<ItemType>(void);
~UnsortedList<ItemType>(void);
bool IsFull() const;
int LengthIs() ;
void MakeEmpty();
void GetItem(ItemType& item, bool& found);
void InsertItem(ItemType itm);
void DeleteItem(ItemType itm);
void ResetList();
void GetNextItem(ItemType& itm);
private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
Class Implementation
#include "UnsortedList.h"
template <class ItemType>
UnsortedList<ItemType>::UnsortedList(void)
{
length = 0;
listData = NULL;
}
template <class ItemType>
UnsortedList<ItemType>::~UnsortedList(void)
{
delete currentPos;
delete listData;
}
template <class ItemType>
bool UnsortedList<ItemType>::IsFull() const
{
try
{
location = new NodeType<ItemType>;
delete location;
return false;
}
catch(std::bad_alloc exception)
{
return true;
}
}
template <class ItemType>
int UnsortedList<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void UnsortedList<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while ( listData != NULL )
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
template <class ItemType>
void UnsortedList<ItemType>::GetItem(ItemType& itm, bool& found)
{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = ( location != NULL );
while ( moreToSearch && !found )
{
if ( itm == location->info )
{
found = true;
itm = location->info;
}
else
{
location = location->next;
moreToSearch = ( location != NULL );
}
}
}
template <class ItemType>
void UnsortedList<ItemType>::InsertItem(ItemType itm)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = itm;
location->next = listData;
listData = location;
length++;
}
template <class ItemType>
void UnsortedList<ItemType>:eleteItem(ItemType itm)
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
if ( item == listData->info )
{
tempLocation = location;
listData = listData->next;
}
else
{
while ( !(itm == location->next->info ) )
{
location = location->next;
tempLocation = location->next;
location->next = ( location->next)->next;
}
delete tempLocation;
length--;
}
}
template <class ItemType>
void UnsortedList<ItemType>::ResetList()
{
currePos = NULL;
}
template <class ItemType>
void UnsortedList<ItemType>::GetNextItem(ItemType& itm)
{
if ( currentPos == NULL )
{
currentPos = listData;
}
else
{
currentPos = currentPos->next;
}
itm = currentPos->info;
}
Main Driver
#include "UnsortedList.h"
#include <iostream>
using namespace std;
//void PrintList(UnsortedList<int>& list);
int main()
{
UnsortedList<int> list1;
//int itm1;
////fill the first list
//itm1 = 1;
//list1.InsertItem(itm1);
//itm1 = 3;
//list1.InsertItem(itm1);
//itm1 = 5;
//list1.InsertItem(itm1);
//cout << "Printing list 1:" << endl;
//PrintList(list1);
return 0;
}
// void PrintList(UnsortedList<int>& list)
//// Pre: list has been initialized.
//// dataFile is open for writing.
//// Post: Each component in list has been written to dataFile.
//// dataFile is still open.
//{
// int length;
// int item;
// list.ResetList();
// length = list.LengthIs();
// cout << "List Items: " << endl;
// for (int counter = 1; counter <= length; counter++)
// {
// list.GetNextItem(item);
// cout << item << endl;
// }
// cout << endl;
//}
I cant seem to figure out why. If I compile the class file without
referencing it in the int main() it will compile but once I try to createon
abject of the class type I get:
Recursive error LNK2019: unresolved external symbol "public: __thiscall
UnsortedList<int>::~UnsortedList<int>(void)" (??1?$UnsortedList@H@@QAE@XZ)
referenced in function _main
Recursive error LNK2019: unresolved external symbol "public: __thiscall
UnsortedList<int>::UnsortedList<int>(void)" (??0?$UnsortedList@H@@QAE@XZ)
referenced in function _main
Recursive is the name of the project.
Please help.
Thanks
Header File
#pragma once
template <class ItemType>
struct NodeType;
template <class ItemType>
class UnsortedList
{
public:
UnsortedList<ItemType>(void);
~UnsortedList<ItemType>(void);
bool IsFull() const;
int LengthIs() ;
void MakeEmpty();
void GetItem(ItemType& item, bool& found);
void InsertItem(ItemType itm);
void DeleteItem(ItemType itm);
void ResetList();
void GetNextItem(ItemType& itm);
private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
Class Implementation
#include "UnsortedList.h"
template <class ItemType>
UnsortedList<ItemType>::UnsortedList(void)
{
length = 0;
listData = NULL;
}
template <class ItemType>
UnsortedList<ItemType>::~UnsortedList(void)
{
delete currentPos;
delete listData;
}
template <class ItemType>
bool UnsortedList<ItemType>::IsFull() const
{
try
{
location = new NodeType<ItemType>;
delete location;
return false;
}
catch(std::bad_alloc exception)
{
return true;
}
}
template <class ItemType>
int UnsortedList<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void UnsortedList<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while ( listData != NULL )
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
template <class ItemType>
void UnsortedList<ItemType>::GetItem(ItemType& itm, bool& found)
{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = ( location != NULL );
while ( moreToSearch && !found )
{
if ( itm == location->info )
{
found = true;
itm = location->info;
}
else
{
location = location->next;
moreToSearch = ( location != NULL );
}
}
}
template <class ItemType>
void UnsortedList<ItemType>::InsertItem(ItemType itm)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = itm;
location->next = listData;
listData = location;
length++;
}
template <class ItemType>
void UnsortedList<ItemType>:eleteItem(ItemType itm)
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
if ( item == listData->info )
{
tempLocation = location;
listData = listData->next;
}
else
{
while ( !(itm == location->next->info ) )
{
location = location->next;
tempLocation = location->next;
location->next = ( location->next)->next;
}
delete tempLocation;
length--;
}
}
template <class ItemType>
void UnsortedList<ItemType>::ResetList()
{
currePos = NULL;
}
template <class ItemType>
void UnsortedList<ItemType>::GetNextItem(ItemType& itm)
{
if ( currentPos == NULL )
{
currentPos = listData;
}
else
{
currentPos = currentPos->next;
}
itm = currentPos->info;
}
Main Driver
#include "UnsortedList.h"
#include <iostream>
using namespace std;
//void PrintList(UnsortedList<int>& list);
int main()
{
UnsortedList<int> list1;
//int itm1;
////fill the first list
//itm1 = 1;
//list1.InsertItem(itm1);
//itm1 = 3;
//list1.InsertItem(itm1);
//itm1 = 5;
//list1.InsertItem(itm1);
//cout << "Printing list 1:" << endl;
//PrintList(list1);
return 0;
}
// void PrintList(UnsortedList<int>& list)
//// Pre: list has been initialized.
//// dataFile is open for writing.
//// Post: Each component in list has been written to dataFile.
//// dataFile is still open.
//{
// int length;
// int item;
// list.ResetList();
// length = list.LengthIs();
// cout << "List Items: " << endl;
// for (int counter = 1; counter <= length; counter++)
// {
// list.GetNextItem(item);
// cout << item << endl;
// }
// cout << endl;
//}