Any ideas as to why I keep getting these error messages:
Code:
excercise4.cpp: In member function `void q_type::q(int)':
excercise4.cpp:31: error: invalid types `int[int]' for array subscript
excercise4.cpp: In member function `int q_type::deq()':
excercise4.cpp:42: error: invalid types `int[int]' for array subscript
When trying to compile this:
Code:
/* Goal: create a class 'queue' that maintains a circular queue of integers. make the queue size
100 integers long, and demonstrate its use in a main function */
#include <iostream>
using namespace std;
#define SIZE 100
class q_type {
int queue; //hold the queue
int head, tail; //indexes of the head & tail of the queue
public:
void init(); //init the queue
void q(int num); //store the queue
int deq(); //retrieve
};
//init the queue
void q_type::init() {
head = tail = 0;
}
//place a value on the queue
void q_type::q(int num) {
if(tail + 1 == head || (tail + 1 == SIZE && !head)) {
cout << "Queue is full.";
return;
}
tail++;
if(tail == SIZE) tail = 0; //cycle
queue[tail] = num;
}
//remove a value from the queue
int q_type::deq() {
if(head == tail) {
cout << "Queue is empty.";
return 0;
}
head++;
if (head == SIZE) head = 0; //cycle
return queue[head];
}
int main() {
q_type q1, q2; //initialize 2 queues
int i;
q1.init();
q2.init();
for(i = 0; i <= 10; i++) {
q1.q(i);
q2.q(i * i);
}
for(i = 0; i <= 10; i++) {
cout << "Dequeue 1: " << q1.deq() << "\n";
cout << "Dequeue 2: " << q2.deq() << "\n";
}
return 0;
}
And don't worry - this is not for class or anything. I'm independently trying to (finally) learn C++, but I keep getting jammed up. Probably because my C knowledge hasn't been used in so long.