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
}
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
}