Can I not do the below code?

class box
{
      public:
        box(){}
        box(int v){x=v;}
        
        ~box(){}
       int getx(){return x;}
       
private:
        int x;

};
____________________________

#include <iostream>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
#include "Q4.h"

int main()
{
    queue<box> B;
    
    int a;
    cout<<"How many boxes would you like to Generate: ";
    cin>>a;
    
    srand((unsigned)time(0));
    int random_integer;
    int lowest=1, highest=100;
    int range=(highest-lowest)+1;
    for(int index=0; index<a; index++)
    {
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    
            B.push(random_integer);
    }
     while (!B.empty())
     {
        cout << "Box "<<a<<" contains: "<< B.front()<<endl;
        B.pop();
     }

Everytime I try to compile and run it gives me an error with the .front()

http://www.cplusplus.com/reference/stl/queue/
it's push_back(), not push(), and pop_front(), not pop(). the same methods work with vector (and push_back puts the data on element 0).

if you want a queue of int you should do
queue<int>
if you want a queue of class box you should do
queue<class box>
because you want the type class box.

for a single variable you want to use the first kind. for multiple variables you want to use a class within queue, such as if your box has multiple dimensions it needs to store (L, W, H or x, y).


you also forgot to #include <queue>
your code is also incomplete and indenting is bad. I will check into the error message from what code is available and check back.

Edited 4 Years Ago by jmichae3: n/a

The #include <queue> is right under the #include <iostream> and I answered my own question,but appreciate the response :)

class box
{
	public:
		box() { //if you don't initialize the members, you get garbage when you get!
			m_x=0; 
			m_y=0;
			m_boxnum=0;
		}
		box(int newx){
			m_x=newx; 
			m_y=0; 
			m_boxnum=0;
		}
		box(int newx, int boxnum){
			m_x=newx; 
			m_y=0;
			m_boxnum=boxnum;
		}
		box(int newx, int newy, int boxnum){
			m_x=newx; 
			m_y=newy; 
			m_boxnum=boxnum;
		}
		~box(){}
		
		int getx() {return m_x;}
		void setx(int n) {m_x = n;}
		
		int gety() {return m_y;}
		void sety(int n) {m_y = n;}
		
		int getBoxNum() {return m_boxnum;}
		void setBoxNum(int n) {m_boxnum = n;}
	 
	private:
		int m_x, m_y; //I would use m_x here, but I don't want to break your code
		int m_boxnum;
 
};

 
#include <iostream>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <vector> //vector is more flexible than queue, can work any op from both ends.
#include <iterator>
using namespace std;
//#include "Q4.h"
 
 
void DumpBoxQueue(vector<class box>& boxqueue) {
	vector<class box>::iterator i; //iterators are like a glorified pointer
	cout << "-----Start Box Queue Dump-----" << endl;
	for (i = boxqueue.begin(); i != boxqueue.end(); i++) {
		cout << "Box number " << i->getBoxNum() 
		     << " contains: x="<< i->getx() 
			 << ", y=" << i->gety() 
			 << endl;
	}
	cout << "-----End Box Queue Dump-----" << endl;
}
 
 
 
 
int main()
{
    vector<class box> B;
     
    int numBoxes;
    cout<<"How many boxes would you like to Generate: ";
    cin>>numBoxes;
	
    srand((unsigned)time(0));
    int random_integerx,random_integery;
    int lowest=1, highest=100;
    int range=(highest-lowest)+1;
	box b; //create the variable once - messes with the stack less
    for(int index=1; index <= numBoxes; index++)
    {
		random_integerx = lowest + int(range * rand() / (RAND_MAX + 1.0));
		random_integery = lowest + int(range * rand() / (RAND_MAX + 1.0));
		b.setBoxNum(index + 1);
		b.setx(random_integerx);
		b.sety(random_integery);
		//must create a box before you can push it - maybe can use new I don't know
		B.push_back(b);
    }
	DumpBoxQueue(B);
	/*
    while (!B.empty())
    {
		cout << "Box "<<a<<" contains: "<< B.front()<<endl;
		B.pop_back();
    }
	*/
	return 0;
}

Thu 12/15/2011 1:03:31.90|C:\prj\test\YAHOO|>1
How many boxes would you like to Generate: 5
-----Start Box Queue Dump-----
Box number 2 contains: x=63, y=48
Box number 3 contains: x=39, y=54
Box number 4 contains: x=32, y=15
Box number 5 contains: x=6, y=99
Box number 6 contains: x=7, y=20
-----End Box Queue Dump-----

Thu 12/15/2011 1:04:23.64|C:\prj\test\YAHOO|>


this is example code. I modified the program to give you an idea of what the program can become. you mention boxes and have x as your member variable, so I assume there is a y coordniaate you wanted, so I added it, and a box number.

one small bug in that code - it should be index, not index + 1. maybe instead you should rename index to boxNum.

Edited 4 Years Ago by jmichae3: n/a

This question has already been answered. Start a new discussion instead.