0
#include<iostream>
using namespace std;

class node
{
    public:
    int data;
    node *next;
}*head;

class linkedlist
{
private:
    node *first;
public:
    linkedlist(){first=NULL;}
    linkedlist(int A[], int n);
    ~linkedlist();
};

void linkedlist :: display(){
    node *p = first;
    while(p!=0){
        cout<<p->data<<" ";
        p=p->next;
    }
}

int main(){
    int A[]={2, 3, 4, 5, 6};
    linkedlist l(A,5);
    l.display();
}

I am getting this error :

C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0x98): undefined reference to linkedlist::linkedlist(int*, int)' C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0xaf): undefined reference to linkedlist::~linkedlist()' C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0xc2): undefined reference to `linkedlist::~linkedlist()' collect2.exe: error: ld returned 1 exit status

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
NoobCoder
  • 29
  • 5
  • That's not the error I get. I get "_`error: no declaration matches 'void linkedlist::display()'`_" but if I fix that by adding `void display();` to the class definition I get the same error as you because you haven't implemented the constructor `linkedlist(int A[], int n);` or the destructor `~linkedlist();` – Ted Lyngmo Aug 25 '21 at 10:07
  • You wrote a destructor, but you didn't provide a definition (implementation) of that destructor. – Yksisarvinen Aug 25 '21 at 10:13
  • You’ve declared and used `linkedlist::linkedlist(int*, int)`, but you haven’t defined it. – Biffen Aug 25 '21 at 10:17
  • yah i implemented both now. thank you – NoobCoder Aug 25 '21 at 12:24

1 Answers1

4
  • You've defined void linkedlist::display() but you haven't declared it in your class definition.
  • You've declared the constructor linkedlist(int A[], int n) but you haven't defined it.
  • You've declared the destructor ~linkedlist() but you haven't defined it.

Other notes:

  • The global node* head is pointless.
  • The node type doesn't need to be visible outside the linkedlist class. I suggest hiding it by making the node definition private in linkedlist.
  • The display() member function does not make changes to the linkedlist object. Make such functions const.

Example fixes:

#include <iostream>
#include <utility> // std::exchange

class linkedlist {
private:
    struct node {     // made definition private
        int data;
        node *next;
    };

    node* first;
public:
    linkedlist();
    linkedlist(int A[], int n);
    linkedlist(const linkedlist&) = delete;            // copying not implemented
    linkedlist& operator=(const linkedlist&) = delete; // copying not implemented
    ~linkedlist();
    void add(int value); // a function to add a value to the linked list
    void display() const; // made const 
};

linkedlist::linkedlist() : first(nullptr) {}

linkedlist::linkedlist(int A[], int n) : first(nullptr) {
    for(size_t idx = 0; idx < n; ++idx) {
        add(A[idx]);                     // use the add() member function
    }
}

linkedlist::~linkedlist() {
    while(first) { // delete all nodes
        delete std::exchange(first, first->next);
    }
}

void linkedlist::add(int value) {
    // add a node by linking it in first
    first = new node{value, first};
}

void linkedlist::display() const {
    if(first) {
        std::cout << first->data;
        for(node* p = first->next; p; p = p->next) {
            std::cout << ' ' << p->data;
        }
    }
}

int main(){
    int A[]={2, 3, 4, 5, 6};
    linkedlist l(A, 5);
    l.display();
}

Output

6 5 4 3 2
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Ohh right. i made changes to my code please have a look. its working now...thanks :) – NoobCoder Aug 25 '21 at 12:23
  • @NoobCoder You're welcome! Please don't change your question radically after you've gotten answers. The question + the answer now doesn't make sense and since this is a Q&A site, it won't help anyone else in its current state. I'll revert the change you made. You do have some problems though like `for(int i=1;i – Ted Lyngmo Aug 25 '21 at 12:26
  • ohkay understood i won't do it hereafter. also, im adding first element in the first node itself. – NoobCoder Aug 25 '21 at 12:29
  • @NoobCoder Ah.. I see. That's an unusual way of doing it. I missed that. :) – Ted Lyngmo Aug 25 '21 at 12:31
  • 1
    i just started to learn DSA xD – NoobCoder Aug 25 '21 at 12:46