Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed
1 year ago
.
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
–
–
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
–
–