Creating pointers constructor?

Is it possible to create a pointer constructor and not a default constructor? Cause mine could not read the variables when I declared pointer in my main class

Main class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void initCar(Queue&, Queue&);
Queue *q1, *q2; // Global variable

int main() {
   initCar(*q1, *q2);
}

void initCar(Queue &q1, Queue &q2) {
   Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
	Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
	Car c3 = Car("SCF1006G","Mercedes", "Large Van");
	Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

	q1.enqueue(c1);
	q2.enqueue(c1);
	q2.enqueue(c3);
	q1.enqueue(c4);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q2.enqueue(c2);
	q2.enqueue(c2);
}


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Queue class
#ifndef Queue_H
#define Queue_H

#include "Car.h"
#include <iostream>
#include <string>
#include <cstddef> // for NULL

using namespace std;

const int Q_MAX_SIZE = 20;

class Queue {
private:
	int size; // size of the queue
	Car carQueue[Q_MAX_SIZE];
	int front, rear;
public:
	Queue();
	~Queue();
	bool isEmpty();
	void enqueue(Car c);
	void dequeue(); // just dequeue the last car in the queue
	void dequeue(Car c); // if a certain car wants to go out of the queue midway. 
						 // Condition: Car is not in washing. Meaning is not the 1st item in the queue
	void dequeue(int index); // same as the previous comment
	Car getFront();
	void getCarQueue(Queue);
	int length();
	Car get(int);
	bool isFull();
};

Queue::Queue() {
	size = 0;
	front = 0;
	rear = Q_MAX_SIZE -1;
}

Queue::~Queue() { 
	while(!isEmpty()) {
		dequeue();
	}
}

void Queue::enqueue(Car c) {
	if (!isFull() || !isEmpty()) {
		rear = (rear + 1) % Q_MAX_SIZE; // circular array
		carQueue[rear] = c;
		size++;
	} else {
		cout << "Queue is currently full.\n";
	}
}

void Queue::dequeue() { 

}

void Queue::dequeue(int index) {
	if(!isEmpty()) {
		front = (front + 1) % Q_MAX_SIZE;
		if(front != index) {
			carQueue[index-1] = carQueue[index];
			rear--;
			size--;
		} else {
			cout << "Not allowed to dequeue the first car in the queue.\n";
		}
	} else {
		cout << "There are no cars to dequeue.\n";
	}
}

bool Queue::isEmpty() {
	return size == 0;	
}

bool Queue::isFull() {
	if (size == Q_MAX_SIZE) {
		return true;
	}
	return false;
}

Car Queue::getFront() {
	return carQueue[front];
}

int Queue::length() {
	return size;
}

Car Queue::get(int index) {
	return carQueue[index-1];
}

void Queue::getCarQueue(Queue q) {
	for(int i = 0; i< q.length(); i++) {
		cout << "\nQ No.\tVehicle Number\n";
		cout << i << "\t" << q.get(i).getVehicleNo() << endl;
		i++;
	}
}

#endif 


I need to create Queue::Queue*() constructor if it is possible as I always get an error in my isFull() function(it cannot read my size, front nor rear in isFull() function)...or is there another way to do this? Thanks!
Last edited on
closed account (o3hC5Di1)
Hi there,

I believe the problem lies here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initCar(Queue *q1, Queue *q2) {
   Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
	Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
	Car c3 = Car("SCF1006G","Mercedes", "Large Van");
	Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

	q1.enqueue(c1);
	q2.enqueue(c1);
	q2.enqueue(c3);
	q1.enqueue(c4);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q2.enqueue(c2);
	q2.enqueue(c2);
}


q1 and q2 are pointers. This means they are variables containing a memory address at which another variable is stored. By doing q1.enqueue(c1); you are asking the enqueue function of a memory address, but that's not what you want - you want to call the enqueue function of the object stored at that memory address.

In order to access the value behind the memory address, we need to dereference the pointer.
The dereference operator * can be used to achieve this:

*(q1).enqueue(); //using brackets ensures the desired result

A more common way is to use the arrow operator, which does the dereferencing for you:

q1->enqueue();

Hope that helps.

All the best,
NwN
You haven't initialized q1 and q2 so it is an error to try to dereference them like you do on line 5 (in main). You must make them point to some valid Queue by using new or some other way.
q1 = new Queue();
Hello! I get what you are trying to say but it did not work for my case. I got an error after adding *(q1).enqueue();
Last edited on
You have to put the asterisk inside the parenthesis.
closed account (o3hC5Di1)
Hold on - I just took a closer look at your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void initCar(Queue&, Queue&);
Queue *q1, *q2; // Global variable

int main() {
   initCar(*q1, *q2);
}

void initCar(Queue &q1, Queue &q2) {
   Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
	Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
	Car c3 = Car("SCF1006G","Mercedes", "Large Van");
	Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

	q1.enqueue(c1);
	q2.enqueue(c1);
	q2.enqueue(c3);
	q1.enqueue(c4);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q2.enqueue(c2);
	q2.enqueue(c2);
}


This is correct, as you're passing the objects by reference, not as a pointer.
The issue is probably more related to what Peter said:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initCar(Queue&, Queue&);
Queue *q1, *q2; // Global variable

int main() {

    try
   {
        q1 = new Queue();
        q2 = new Queue();
        initCar(*q1, *q2);
    }
    catch (std::bad_alloc &e)
    {
        std::cout << "No memory available";
    }
}


Hope that helps.

All the best,
NwN
You have to put the asterisk inside the parenthesis. Meaning?
// Edit: YEAH it works! I solved it by using new in the main method! Thanks both NwN and Peter87 :D
Last edited on
Meaning *(q1).enqueue(); should be (*q1).enqueue();.

I don't know if you have a good reason to use pointers but it's often easier to avoid pointers and new if possible. You could just create the queues inside the main function like this
1
2
Queue q1, q2;
initCar(q1, q2); // if initCar takes references. initCar(&q1, &q2); if it takes pointers. 

and get rid of the global variables.
according to what you said, I think i should use
initCar(q1, q2); // if initCar takes references..

global functions are not included right?
eg bool login(string, string, List<Worker>&);
only for string username, Queue q1 etc..
Topic archived. No new replies allowed.