Queue of objects

Hello all,

I am trying to create a queue with a maximum of 10 instances that will store an object containing a string and integer.

My program structure so far:

bookqueue.h (is the class responsible for creation of a queue)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

#include <string>
#include <iostream>
#include "userclass.h"
using namespace std;

class bookqueue
{
	private:
		int contents[10];
		string bookname;
		int queuesize;
		int front;
		int back;

	public:
		bookqueue(int queuesize =10);
		bool Empty() const;
		bool Full() const;
		bool Remove(userclass);
		bool Add(userclass);
};


userclass.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include <iostream>
#include <string>

using namespace std;

class userclass
{

	public:
		int userid;
		string surname;

};



In my main program I want to take my object called "newuser" from userclass and pass it to my queue add function which is:

1
2
3
4
5
6
7
8
9
10
11
bool bookqueue::Add (userclass)
{
	if (Full()) 
    return false;
  else
  {
    contents[back] = userclass.userid;
    back = (back + 1) % (queuesize + 1);
    return true;
  }
}


I am stuck from here onwards with the error:

1. IntelliSense:a nonstatic member reference must be relative to a specific object

How do I get my queue to store this object "newuser"? Any suggestions or directions to go in would be appreciated.

Will I need a template class for my queue?

Thanks
First question: Do you have to create your own queue? Could you not use an STL one?

Assuming you have to use your own queue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Book {
    Book() : name(""), userId(-1) { }

    string name;
    int userId;
};

class BookQueue {
public:
    BookQueue() : books(0) { }

    bool addBook(Book book) { 
        if (books == 10)
            return false;

        bookQueue[books] = book;
        books++;
        return true;
    }

private:
    Book bookQueue[10];
    int books;
};
1
2
3
4
5
6
7
8
    bool addBook(Book book) { 
        if (books == 10) //books count the number of instances
            return false;

        bookQueue[books] = book; //books works as an index
        books++;
        return true;
    }
You are giving too much responsibility to books. You need to consider the remove operation.
Last edited on
@ne555: When books == 10, it's pointing at the 11th index. Because this index is invalid due to the size of our array being 10 this is completely valid.

The index and count in this situation are the same thing. I've not offered a solution, but an illustration of how the objects could be built and put into a static array. The original post shows much confusion about the techniques required to start putting the final functionality together. I've offered some code that should help with this.

Edit: ne555 changed his reply.



Last edited on
If I was giving a solution for a Queue, I'd use the STL queue.

To do it with the code I've provided you need 2 ints. index and size so you can wrap the index around the array without having to copy objects continuously.
Ok I have changed my strategy to this but It does not seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class bookqueue
{
	private:
		userclass contents[10];
		string bookname;
		int queuesize;
		int front;
		int back;

	public:

		bool Empty() const;
		bool Full() const;
		bool Remove();
		bool Add();


and for add operation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool bookqueue::Add ()
{
	
	if (Full()) 
    return false;
  else
  {
		cout << "Please enter surname of user" << endl;
	cin >> contents[back].surname;

		cout << "Please enter the user's Library ID" << endl;
		cin >> contents[back].userid;
	back = (back + 1) % (queuesize + 1);
    return true;
  }
}



However it crashes when I attempt to CIN to contents[back].surname , is there something wrong with my initialisation?

Thanks for help so far :)

loosebruce wrote:
is there something wrong with my initialisation?
likely.
1
2
3
        int queuesize;
        int front;
        int back;
should be initialized to 0 in your constructor. Does 'userclass' initialize it's content correctly?
For my class "userclass" there is no initialization, should I initialize them?

1
2
3
4
5
6
7
8
9
class userclass
{

	public:
		int userid;
		string surname;

};


Thanks for the help!
yes, anything that doesn't initialize itself should be initialized explicitly. That avoids problems
Topic archived. No new replies allowed.