Witam wszystkich :)
Właśnie próbowałem wprowadzić listę jednokierunkową do realiów języka C++, oto efekty
plik z "mainem"
#include "stdafx.h"
#include <iostream>
#include "unorderedLinkedList.h"
#include "linked_list.h"
using namespace std;
int main()
{
int num = 0;
unorderedLinkedList<int> a;
cout<<"Program uses linked list to implement dynamic array\nEnter your numbers ending with \'s\':";
while(cin)
{
cin>>num;
a.insertLast(num);
}
cin.clear();
cout<<"Your numbers are: \n";
a.print();
system("PAUSE");
return 0;
}
plik linked_list.h
#ifndef H_linked_list
#define H_linked_list
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
//Default constructor.
//Postcondition: current = NULL;
linkedListIterator(nodeType<Type> *ptr);
//Constructor with a parameter
//Postcondition: current = ptr;
Type operator*();
//Function to overload the dereferencing operator *.
//Postcondition: Returns the info contained in the node
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
private:
nodeType<Type> *current;
};
template <class Type>
class linkedListType
{
public:
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty, otherwise it returns false
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
void destroyList();
//Function to delete all the nodes in the list.
//Postcondition: first = NULL, last = NULL , count = 0;
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
//Overload the assignment operator
void initializeList();
//Initialize the list to an empty state.
//Postcondition: Returns true if the list is empty, otherwise it returns false
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list
//Postcondition: The value of count is returned
Type front() const;
//Function to return the first element of the list.
//Precondition: The list must not be empty
//Postcondition: If the list is empty program terminates; otherwise , the first element of the list is returned
Type back() const;
//Function to return the last element of the list.
//Precondition: The list must not be empty
//If the list is empty program terminates; otherwise the last element of the list is returned
virtual bool search(const Type& searchItem) const =0;
//Function to determine wheter searchItem is in the list.
//Postcondition: Returns true if searchItem is in the list, otherwise the value false is returned.
virtual void insertFirst(const Type& newItem) = 0;
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
//inserted at the beginning of the list,
//last points to the last node in the list,
//and count is incremented by 1
virtual void insertLast(const Type& newItem) = 0;
//Function to insert newItem at the end of the list.
//Postcondition: first points to the newList, newItem is
//inserted at the end of the list,
//last points to the last node in the list,
//and count is incremented by 1
virtual void deleteNode(const Type& deleteItem) = 0;
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing deleteItem is deleted from the list.
//first points to the first node, last
//points to the last node of the updated
//list, and count is decremented by 1.
linkedListIterator<Type> begin();
//Function to return an iterator at the begining of the linked list
//Postcondition: Returns an iterator such that current is set to first.
linkedListIterator<Type> end();
//Function to return an iterator one element past the
//last element of the linked list.
//Postcondition: Returns an iterator such the current is set tu NULL.
linkedListType(const linkedListType<Type>& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count;
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and assigned to this list.
};
#endif
plik z definicjami funkcji w klasach
#include "stdafx.h"
#include "linked_list.h"
#include <cassert>
#include <iostream>
#include "unorderedLinkedList.h"
using namespace std;
template<class Type>
linkedListIterator<Type>::linkedListIterator()
{
current=NULL;
}
template<class Type>
linkedListIterator<Type>::linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template<class Type>
Type linkedListIterator<Type>::operator*()
{
return current->info;
}
template<class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
current = current->link;
return *this;
}
template<class Type>
bool linkedListIterator<Type>::operator==(const linkedListIterator<Type>& right) const
{
return current==right.current;
}
template<class Type>
bool linkedListIterator<Type>::operator!=(const linkedListIterator<Type>& right) const
{
return !(current==right);
}
template<class Type>
bool linkedListType<Type>::isEmptyList() const
{
return (first==NULL);
}
template<class Type>
linkedListType<Type>::linkedListType()
{
first=NULL;
last=NULL;
count=0;
}
template<class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while (first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template<class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current;
current=first;
while(current != NULL )
{
cout<<current->info<<" ";
current=current->link;
}
}
template <class Type>
int linkedListType<Type>::length() const
{
return count;
}
template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != NULL );
return first->info;
}
template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != NULL) ;
return last->info;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
linkedListIterator<Type> temp(first);
return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
linkedListIterator<Type> temp(NULL);
return temp;
}
template <class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode;
nodeType<Type> *current;
if (first != NULL)
destroyList();
if (otherList.first == NULL)
{
first=NULL;
last=NULL;
count=0;
}
else
{
current = otherList.first;
count = otherList.count;
first = new nodeType<Type>;
first->info = current->info;
first->link = NULL;
last = first;
current = current->link;
while (current != NULL)
{
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
template <class Type>
linkedListType<Type>::~linkedListType()
{
destroyList();
}
template <class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
first = NULL;
copyList(otherList);
}//end copy constructor
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
if (this != &otherList)
{
copyList(otherList);
}
return *this;
}
template <class Type>
bool unorderedLinkedList<Type>::
search(const Type& searchItem) const
{
nodeType<Type> *current; //pointer to traverse the list
current=first;
while (current != NULL && !found)
if (current->info == searchItem)
return true;
else
current = current->link;
return false;
}
template <class Type>
void unorderedLinkedList<Type>::
insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if (last == NULL)
last = newNode;
}
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
if( first == NULL)
{
first = newNode;
last = newNode;
count++;
}
else
{
last->link = newNode;
last = newNode;
count++;
}
}
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if(first == NULL) //Case 1; the list is empty
cout<< "Cannot delete from an empty list"<<endl;
else
{
if (first->info == deleteItem)
{
current = first;
first = first->link;
count--;
if (first == NULL)
last = NULL;
delete current;
}
else
{
found = false;
trailCurrent = first;
current = first->link;
while (current != NULL && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current -> link;
}
else
found = true;
}
if (found)
{
trailCurrent->link = current->link;
count--;
if (last == current)
last=trailCurrent;
delete current;
}
else
cout<< "The item to be deleted is not in the list" << endl;
}
}
}
Bym zapomniał- klasa unorderedLinkedList
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include "linked_list.h"
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode (const Type& deleteItem);
};
#endif
Wyskakuje błąd
1>linked_list_ADT.obj : error LNK2028: unresolved token (0A000335) "public: __thiscall linkedListType<int>::~linkedListType<int>(void)" (??1?$linkedListType@H@@$$FQAE@XZ) referenced in function "public: __thiscall unorderedLinkedList<int>::unorderedLinkedList<int>(void)" (??0?$unorderedLinkedList@H@@$$FQAE@XZ)
1>linked_list_ADT.obj : error LNK2028: unresolved token (0A000336) "public: __thiscall linkedListType<int>::linkedListType<int>(void)" (??0?$linkedListType@H@@$$FQAE@XZ) referenced in function "public: __thiscall unorderedLinkedList<int>::unorderedLinkedList<int>(void)" (??0?$unorderedLinkedList@H@@$$FQAE@XZ)
1>linked_list_ADT.obj : error LNK2028: unresolved token (0A000338) "public: void __thiscall linkedListType<int>::print(void)const " (?print@?$linkedListType@H@@$$FQBEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>linked_list_ADT.obj : error LNK2028: unresolved token (0A000339) "public: virtual void __thiscall unorderedLinkedList<int>::insertLast(int const &)" (?insertLast@?$unorderedLinkedList@H@@$$FUAEXABH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>linked_list_ADT.obj : error LNK2019: unresolved external symbol "public: __thiscall linkedListType<int>::~linkedListType<int>(void)" (??1?$linkedListType@H@@$$FQAE@XZ) referenced in function "public: __thiscall unorderedLinkedList<int>::unorderedLinkedList<int>(void)" (??0?$unorderedLinkedList@H@@$$FQAE@XZ)
1>linked_list_ADT.obj : error LNK2019: unresolved external symbol "public: __thiscall linkedListType<int>::linkedListType<int>(void)" (??0?$linkedListType@H@@$$FQAE@XZ) referenced in function "public: __thiscall unorderedLinkedList<int>::unorderedLinkedList<int>(void)" (??0?$unorderedLinkedList@H@@$$FQAE@XZ)
1>linked_list_ADT.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall unorderedLinkedList<int>::search(int const &)const " (?search@?$unorderedLinkedList@H@@UBE_NABH@Z)
1>linked_list_ADT.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall unorderedLinkedList<int>::insertFirst(int const &)" (?insertFirst@?$unorderedLinkedList@H@@UAEXABH@Z)
1>linked_list_ADT.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall unorderedLinkedList<int>::insertLast(int const &)" (?insertLast@?$unorderedLinkedList@H@@UAEXABH@Z)
1>linked_list_ADT.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall unorderedLinkedList<int>::deleteNode(int const &)" (?deleteNode@?$unorderedLinkedList@H@@UAEXABH@Z)
1>linked_list_ADT.obj : error LNK2019: unresolved external symbol "public: void __thiscall linkedListType<int>::print(void)const " (?print@?$linkedListType@H@@$$FQBEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>linked_list_ADT.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall unorderedLinkedList<int>::insertLast(int const &)" (?insertLast@?$unorderedLinkedList@H@@$$FUAEXABH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\Tomek\documents\visual studio 2010\Projects\linked_list_ADT\Debug\linked_list_ADT.exe : fatal error LNK1120: 12 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.11
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Wygląda na to że linker nie może znaleźć definicji funkcji. Pytanie tylko dlaczego?