Problem with class

why is my count variable being set to zero

Page 1 of 1

2 Replies - 314 Views - Last Post: 29 March 2009 - 02:46 PM Rate Topic: -----

#1 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Problem with class

Posted 29 March 2009 - 02:31 PM

For some reason my count variable is being set to zero even when i try to initialize it with a different value

here is my .h

const int MAX_SIZE = 10;
class PackingQueue
{
public:
	PackingQueue(void);
	~PackingQueue(void);
	bool dequeue(int & data);
	bool enqueue(int data);
	bool empty();
	bool full();
	int info[MAX_SIZE];
	


	

};



here is my implementation
#include "PackingQueue.h"

int count = 6;

PackingQueue::PackingQueue(void)
{
	

	info[0] = 32;
	info[1] = 40;
	info [2] = 35;
	info [5] = 54;
	info [6] = 6;
	info [7] = 7;

	

}



bool PackingQueue::dequeue(int & data)
{
	if( empty() )
		return false;

	data = info[0];
	count = count - 1;

	for(int i = 0; i < count; i++)
		info[i] = info[i + 1];

	return true;

}


bool PackingQueue::empty()
{
	if(count = 0)
		return true;

	return false;
}


bool PackingQueue::enqueue(int data)
{
	if(full())
		return false;

	info[count] = data;
	count++;



	return true;
}

bool PackingQueue::full()
{
	if(count == MAX_SIZE)
		return true;

	
	return false;
}


PackingQueue::~PackingQueue(void)
{
}



here is a test program to test the class

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

const int MAX = 10;


void main()
{


	PackingQueue queue;
	int data;

	queue.dequeue(data);

	cout << data<<endl;

	queue.dequeue(data);
	cout << data <<endl;





}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with class

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem with class

Posted 29 March 2009 - 02:45 PM

You are overwriting count's value here:
bool PackingQueue::empty()
{
	if(count = 0)
		return true;


should be
bool PackingQueue::empty()
{
	if(count == 0)
		return true;


Was This Post Helpful? 1
  • +
  • -

#3 luckyman76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: Problem with class

Posted 29 March 2009 - 02:46 PM

Here's one thing...in your code you've got:

bool PackingQueue::empty()
{
	if(count = 0)  // This should be if (count == 0) 
		return true;

	return false;
}

I wouldn't think it would compile this way because you're using the assignment operator '=' instead of '=='


[
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1