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.

2
  • You don't build with ListElement.cpp? Commented Mar 8, 2015 at 17:30
  • Though there are plenty of direct duplicates of this (many end up closed or deleted), the tail end of this answer likely describes your problem, a failure to compile and link the additional .cpp file. The related question for that answer is outstanding, btw. and worth remembering and reviewing. Commented Mar 8, 2015 at 17:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.