0

I need to implement a linked list and I have trouble finding an error in my code. I know that unsolved external symbol means that I probably have a function declaration and no implementation, but I looked through my code and I can't see what's missing.

The error that I get is LNK2019: unresolved external symbol "public: __thiscall ListElement::ListElement(int)" (??0ListElement@@QAE@H@Z) referenced in function "public: __thiscall List::List(void)" (??0List@@QAE@XZ)

List.h

#ifndef LIST_H
#define LIST_H
#include "ListElement.h"

class List {
public:

ListElement *head; // wskaźnik na pierwszy element;
ListElement first = NULL; // pierwszy element
int total = 0;

List();

int get(int i);

void addFirstElement(int x);

void addLastElement(int x);

void addRandomElement(int x);

void removeFirstElement();

void removeLastElement();

void removeRandomElement();

private:

};

#endif

List.cpp

#include "stdafx.h"
#include "List.h"

List::List(){
    head = &first;
}

int List::get(int i){ return -1; }

void List::addFirstElement(int x){
    ListElement newEl = ListElement(x);
    newEl.next = &first;
    head = &newEl;
    total++;
}

void List::addLastElement(int x){
    ListElement* p = first.next;
    while (p != NULL){
        p = p->next;
    }
    ListElement newEl(x);
    p = &newEl;
}

void List::addRandomElement(int x){
    int pos = rand() % total;
    //TODO
}

void List::removeFirstElement(){
}

void List::removeLastElement(){
    ListElement last = get(total - 1);
    delete &last;
    total--;
    last = get(total - 1);
    last.next = NULL;
}

void List::removeRandomElement(){
}

ListElement.h

#ifndef LIST_ELELENT_H
#define LIST_ELEMENT_H

class ListElement{
public:
ListElement(int x);
int data; // wartość
ListElement * next; // wskaźnik następnego elementu
private:
};
#endif

ListElement.cpp

#include "ListElement.h"
ListElement::ListElement(int x){
data = x;
}

I realize this is probably a duplicate question, but none of the answers I found helped me solve this.

Asalas77
  • 612
  • 4
  • 15
  • 26
  • You don't build with `ListElement.cpp`? – Some programmer dude Mar 08 '15 at 17:30
  • Though there are plenty of direct duplicates of this (many end up closed or deleted), the tail end of [this answer](http://stackoverflow.com/a/12574400/1322972) likely describes your problem, a failure to compile and link the additional .cpp file. The [related question](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) for that answer is *outstanding*, btw. and worth remembering and reviewing. – WhozCraig Mar 08 '15 at 17:49

1 Answers1

1

I guess you have forgotten to link all your *.cpp files together.

Try it like this: g++ List.cpp ListElement.cpp YourMain.cpp -o ListProgramm