Undefined reference to...

Hello, I was trying to make a simple queue by using classes.
Problem is, I can't seem to get it working.
When I compile it into a .o file, it all works, but when I compile the .o file, I get this error:

classQueue.o: In function `Queue::enqueue(int)':
classQueue.cpp:(.text+0x23): undefined reference to `Node::Node()'
collect2: ld returned 1 exit status

This is my .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "classQueue.h"

void Queue::enqueue(int value)
{
    Node *newNode = new Node;
    newNode->value = value;

    if(Queue::head == NULL)
    {
        Queue::head = newNode;
        Queue::tail = newNode;
    }
}
  
int main()
{
    Queue *Q;
    Q->head = NULL;
    Q->enqueue(2);
} 

And this is my custom header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef INC_CLASSQUEUE_H
#define INC_CLASSQUEUE_H

class Node
{
    public:
        Node();
        Node *next;
        int value;
        ~Node();
};

class Queue
{   
    public:
        Queue();
        void enqueue(int);
        Node *head;
        Node *tail;
        ~Queue();

};

#endif //INC_CLASSQUEUE_H 

Help would be very much appreciated,
I need a fast response though, I have an exam on this tomorrow,
And I'm having some trouble with classes.
Where is there the definition of constructor Node::Node() in your code?
The error message is quite specific. In file classQueue.h the constructor Node(); is declared. But there is no definition for the body of that function.
Alright, thanks for your answers.
I'll try to fix it :).
So after some work (basically reviewing classes in its entirety)
I have got this, but it still has a segmentation fault:

.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "classQueue.h"

void Queue::enqueue(int value)
{
    Node *newNode;
//    newNode->value = value;

    if(Queue::head == NULL) //gdb says the Segmentation fault is here.
    {
        Queue::head = newNode;
        Queue::tail = newNode;
    }
}

int main()
{
    Queue *Q;
    Q->enqueue(2);
}

.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef INC_CLASSQUEUE_H
#define INC_CLASSQUEUE_H

class Node
{
    private:
       Node *next;
       int value;

     public:
         Node()
        {
            next = NULL;
            value = 0;
        };    
        ~Node();
};

class Queue
{
    private:
        Node *head;
        Node *tail;

    public:
        Queue()
        {
            head = NULL;
            tail = NULL;
        }

        void enqueue(int);
        ~Queue();
};
#endif //INC_CLASSQUEUE_H 


I know it's because the head pointer doesn't exist, but how do I fix it?
The problem is that you did not allocate an object of type Queue.

After these statements

1
2
3
4
5
int main()
{
    Queue *Q;
    Q->enqueue(2);
}


Q has undefined value. I think that you should write

1
2
3
4
5
int main()
{
    Queue Q;
    Q.enqueue(2);
}


Also value of the argument in enqueue is not used.
Topic archived. No new replies allowed.