Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1>lacp4.obj : error LNK2019: unresolved external

Status
Not open for further replies.

spazz41

Technical User
Sep 13, 2005
149
0
0
US
I am getting the following error:
1>lacp4.obj : error LNK2019: unresolved external symbol "public: int __thiscall LinkedList<int>::size(void)" (?size@?$LinkedList@H@@QAEHXZ) referenced in function _main
1>C:\Documents and Settings\Linc.LINC\My Documents\Visual Studio 2005\Projects\lacp4\Debug\lacp4.exe : fatal error LNK1120: 1 unresolved externals

This is my first time working with templates so I am sure I missed something easy. I will include the code below. Any help would be great.

lacp4.cpp
#include "linkedlist.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using std::cin;
using std::ifstream;
using std::string;


int main(int argc, char *argv[]) {
printf("Lincoln Coleman\n\nPlease input the name of the file you would like to open(or Q to quit): ");
string file_input;
cin >> file_input;
LinkedList <int> linkedlistexample; //declares my linked list type
int err_result = 0;
while (!(file_input != "Q" ^ file_input != "q"))
{
ifstream opendata (file_input.c_str());
if (opendata.is_open())
{
string input_line; //holds value from the next line
int add_to_stack; //holds number to store in queue
while (!opendata.eof())
{
getline(opendata,input_line);
add_to_stack = atoi(input_line.c_str()); //convert to int
if (add_to_stack >= INT_MIN && add_to_stack <= INT_MAX)
linkedlistexample.size();
}
//print contents of both
}else
{
printf("Error opening file... check filename and try again.");
}

printf("\nPlease input the name of the file you would like to open(or Q to quit): ");
cin >> file_input;
opendata.close();
}
return 0;
}

linkedlist.h
#include <string>
template <class T>
class LinkedList {

private:
struct Node{
T value;
Node * next;

Node(){ //constructor
next = NULL;
}
Node(T v, Node * n = NULL){ //constructor that takes a value and/or a pointer to next
value = v;
next = n;
}
};
struct Node *head; //pointer to hold node at the top
struct Node *tail; //pointer to hold node at the bottom
struct Node *p; //used for pointer cleanup
int count; //int to hold the number of elements in the queue

public:
LinkedList(){ //constructor
head = NULL;
tail = NULL;
p = NULL;
count = 0;
}
~LinkedList(){//destructor to cleanup any memory that might be left in the class
while (head != NULL){
p = head->next;
delete head;
head = p;
}
p = NULL;
}
int insert(T); //appends a value to the linked list
int size(); //returns current size of linked list
void traverse(void (*func)(T));//generic traverse function

};

linkedlist.cpp
#include "linkedlist.h"
template <class T> int LinkedList<T>::insert(T bottom){
if (count == 0){
head = new Node(bottom);
tail = head; //sets the head and tail to be the first node
}else{
tail->next = new Node(bottom);
tail = tail->next; //sets new tail
}
count++; //incriments the node counter
return 0; //returns success
}

template <class T> int LinkedList<T>::size(){
return count;
}
template <class T> void LinkedList<T>::traverse(void (*func)(T)){//generic traverse function that takes an int
struct Node *t_head = head; //temporary storage for the HEAD
while (t_head != NULL){
p = t_head->next;
func(t_head->data); //passes data to the function
t_head = p;
}
p = NULL ; //returns p to null state
}
 
All your templated functions need to be in the .h file, otherwise the compiler has no way of knowing how to instantiate them for whatever type(s) you choose.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
ah ok, so there is no reason to split them into .cpp and .h for a template then. good to know, thanks :)
 
Now that I have that working, can anyone tell me how to get the address of a member function of a class, so I can pass it by reference ?
 
> can anyone tell me how to get the address of a member function of a class


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top