LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-16-2004, 09:56 PM   #1
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
invalid types int[int] for array subscript


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.
 
Old 11-16-2004, 10:31 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Code:
    int queue;

    ...

    return queue[head];
That's a problem! You need to declare queue as an array.
 
Old 11-16-2004, 10:34 PM   #3
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Original Poster
Rep: Reputation: 47
* scuzzman is seriously feeling like an idiot right now

How could I miss that? Thanks a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid conversion from `int*' to `socklen_t*' r350 Programming 5 10-02-2011 06:34 PM
passing int array to thread? Thinking Programming 2 09-21-2005 12:00 PM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 01:08 PM
string to int in C h/w Programming 2 12-05-2003 04:47 PM
int arr[2]={23,12,45}; aditya Programming 1 03-03-2003 10:32 AM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration