0

So, from a .csv file i'm creating users, which is from the class User. I'm trying to create a linkedlist, that will contain all users, but i'm getting an error inserting them into the linkedlist wich, by the way is named "ListaLigada". I can't figure it out by myself.

so, this is what i'm getting:

Error 1 error LNK2019: unresolved external symbol "public: void __thiscall ListaLigada::insert(class User)" (?insert@?$ListaLigada@VUser@@@@QAEXVUser@@@Z) referenced in function "void __cdecl genUserLList(class ListaLigada &)" (?genUserLList@@YAXAAV?$ListaLigada@VUser@@@@@Z) C:\Users\Daniel\documents\visual studio 2010\Projects\Radio Station\Radio Station\Auxf.obj Radio Station

If you can identify what i'm doing wrong i'll be gratefull, it has been long 2 days by now.

Here's the code that is related:

ListaLigada.h

    #pragma once
    #ifndef ListaLigada_H
    #define ListaLigada_H

    #include <cstddef>
    #include <cassert>

    template<class T>
    class ListaLigada
    {
    public:
        ListaLigada();
        ListaLigada(T elem);
        ~ListaLigada(void);

            void insert(T elem);
        void remElem(T elem);
        void remElem(int id_elem);
        void searchElem(int id_elem);
        void searchElem(T elem);
        void clear();

    private:
        struct Node{
            T data;
            Node *next;
            Node *prev;
        } *p;

        size_t list_size;
    };


    #endif

ListaLigada.cpp

    #include "ListaLigada.h"
    template<class T>
    ListaLigada<T>::ListaLigada()
    {
        list_size=0;
        this->p=new Node;
        this->p=NULL;
    }

    template<class T>
    ListaLigada<T>::~ListaLigada(void)
    {
        clear();
    }

    template<class T>
    ListaLigada<T>::ListaLigada(T elem)
    {
        node *q, *t;
        if(p == NULL)
        {
            p=new Node;
            p->data= elem;
            p->next= Null;
            list_size++;
        }
        else
        {
            q=p;
            while(q->next!= NULL)
                q= q->next;
            t= new node;
            t->data= elem;
            t->next=Null;
            t->prev=q;
            q->next=t;
            list_size++;
        }
    }

    template <class T>
    void ListaLigada<T>::insert(T elem)
    {
        node *q, *t;
        if(p == NULL)
        {
            p=new Node;
            p->data= elem;
            p->next= NULL;
            list_size++;
        }
        else
        {
            q=p;
            while(q->next!= NULL)
                q= q->next;
            t= new node;
            t->data= elem;
            t->next=NULL;
            t->prev=q;
            q->next=t;
            list_size++;
        }
    }

    template <class T>
    void ListaLigada<T>::remElem(T elem)
    {
        node *q, *t;
        if(p== NULL)
        {
            system("cls");
            cout<<"There's nothing to remove\n";
            _getch();   
        }
        else
        {
            q=p;
            while(q->next != NULL){
                //e se for o unico elemento?
                if(list_size==1)
                {
                    ~ListaLigada();
                //blahblah limpar ficheiros
                    break;
                }
                else
                {
                    if(q->data.getId() == )
                }

            }
        }

    }

    template <class T>
    void ListaLigada<T>::remElem(int id_elem){}

    template <class T>
    void ListaLigada<T>::searchElem(int id_elem){}

    template <class T>
    void ListaLigada<T>::searchElem(T elem){}

    // deletes all the list elements
    template <class T>
    void ListaLigada<T>::clear()
    {
    node *q;
    if( p == NULL )
    return;
    while( p != NULL )
    {
    q = p->next;
    delete p;
    p = q;
    listSize--;
    }
    //assert(listSize==0);
    }

auxf.h

#ifndef AUX_H
#define AUX_H

//#include "includes.h"
#include "ListaLigada.h"
#include "Music.h"
#include "User.h"
#include "RadioStation.h"
#include <conio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <list>

#define MIN_LEN 2

bool strMinLen(string str, int min_len);
void fillField(string &to_fill, const string &what_is);
void checkFiles();
void loadInfo(vector<int> &vec); //carrega ids maximos
void regUser();
void addMusic();
void updateRestoreTxt(const int &line);
void loadRestore();
void updateRestore();
//void genUserLList(list<User> &lista);
void genUserLList(ListaLigada<User>);


#endif

auxf.cpp //i'll post only the funciont that is related

void genUserLList(ListaLigada<User> &lista){
    ifstream ifs;
    ifs.open("users.csv");
    if(ifs){
        int to_read;
        to_read=getNUsers(); //nr of users to read


        string user_line;
        getline(ifs, user_line, ','); //linha cabecalho
        for(int i=0; i<to_read; i++){

            user_line.clear();

            string name, password;
            int age;
            char gender;
            unsigned int id=0;

            getline(ifs, user_line);

            stringstream sstream;
            sstream<<user_line;
            string tmp;
            stringstream sstmp;
            char delim= ',';

            getline(sstream, tmp, delim);
            sstmp<<tmp;
            sstmp>>id;
            sstmp.clear();
            tmp.clear();

            getline(sstream, name, ',');

            getline(sstream, tmp, ',');
            sstmp<<tmp;
            sstmp>>age;
            sstmp.clear();
            tmp.clear();

            getline(sstream, tmp, ',');
            sstmp<<tmp;
            sstmp>>gender;
            sstmp.clear();
            tmp.clear();

            getline(sstream, password, ',');

            User n_user(id, name, age, gender, password);
            /*list<User>::iterator it;
            it=lista.end();
*/
            lista.insert(n_user);
        }
    }
    else{
        cout<<"Lista Ligada ta fdd\n";
        _getch();
    }
}

Thanks in advance. I don't know what to do anymore... I'm reading the users

Bill
  • 5,263
  • 6
  • 35
  • 50
Daniel Mendonça
  • 391
  • 1
  • 3
  • 14
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it? Template implementation not visible](http://stackoverflow.com/a/12574417/902497). – Raymond Chen May 27 '13 at 14:11

2 Answers2

5

When you use a templated class, you need the complete definition, and that includes all methods as well. Therefore you can't split a templated class into header and source file, everything has to be in the header file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You cannot implement template class in other compilation unit then you want to use it.

You should do it all in header or include this implementation into your header to keep it more readable.

The reason is that code for specialized template class is generated when it is used. If you only want to use some well known specialization of template class you can use implementation separated from header and just force those specialization to be created.

Here is some more: Why can templates only be implemented in the header file?

Community
  • 1
  • 1
Yester
  • 652
  • 6
  • 18