Error LNK2091

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>::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;

//}
 
A

Arjun Bijanki [VCPP MSFT]

--------------------
From: "Red" <[email protected]>
Subject: Error LNK2091
Date: Wed, 28 Apr 2004 21:55:53 -0400
Lines: 393
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: ool-18ba8c42.dyn.optonline.net 24.186.140.66
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA06.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.vc:36196
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top