×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

1>lacp4.obj : error LNK2019: unresolved external

1>lacp4.obj : error LNK2019: unresolved external

1>lacp4.obj : error LNK2019: unresolved external

(OP)
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
}

RE: 1>lacp4.obj : error LNK2019: unresolved external

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.

RE: 1>lacp4.obj : error LNK2019: unresolved external

(OP)
ah ok, so there is no reason to split them into .cpp and .h for a template then. good to know, thanks :)

RE: 1>lacp4.obj : error LNK2019: unresolved external

(OP)
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 ?

RE: 1>lacp4.obj : error LNK2019: unresolved external

> can anyone tell me how to get the address of a member function of a class
http://www.newty.de/fpt/index.html

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close