Groups | Blog | Home
all groups > visual c > april 2004 >

visual c : Error LNK2091


Red
4/28/2004 9:55:53 PM
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>::DeleteItem(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;

//}

arjunb NO[at]SPAM online.microsoft.com
5/3/2004 9:50:35 PM

--------------------
[quoted text, click to view]
cpmsftngxa10.phx.gbl!TK2MSFTNGXA06.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP12.phx.gbl
[quoted text, click to view]

No code is generated for templates until they have been instantiated. In
your class implementation, you need to instantiate UnsortedList with int,
in order to produce the function definitions that the main driver requires.
Alternatively, if you don't know the list of types up front (for example,
it's a library with consumers), you could include the class implementation
into the main driver as a header file.

Your example is similar to this:

//test.h
#pragma once
template<class T> class test
{
public:
void func();
};

//test.cpp - implementation
#include "test.h"
template<class T> void test<T>::func()
{
//stuff here
}

//main.cpp - uses test
#include "test.h"
int main()
{
test<int> t;
t.func();
}


And my recommendation to fix it is either:

1) add this line to test.cpp to explicitly instantiate the template for
int: template class test<int>; or,

2) #include "test.cpp" inside main.cpp


--
Arjun Bijanki, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
AddThis Social Bookmark Button