First In First Out Queue Problems

Hey guys, I'm trying to put together a queue program. I've been working off and on with this for weeks and have had absolutely no luck. I've stripped the code down to just it's basic functions and data members, so right now all that exists are constructors and a function that creates new class objects each time a new piece of data is passed to it. Just about all of this code is lifted directly from the assignment sheet that my professor gave me. I can't for the life of me get it to compile. Below is my code, followed by the compiler errors:

1
2
3
4
5
6
7
8
9
10
11
#include "queue.h"
#include "queueItem.h"

int main()
{
	queue myQueue;

	myQueue.addItem("red");
	myQueue.addItem("blue");
	myQueue.addItem("green");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*queue.h*/
#ifndef _QUEUE_H_
#define _QUEUE_H_

#include <iostream>
#include <string>
#include "queueItem.h"

class queue {
public:
	queue();
	void addItem(char *pData);
private:
	queueItem *pHead;
	queueItem *pTail;
	int itemCounter;
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*queue.cpp*/
#include "queue.h"

using namespace std;

queue::queue()
{
	pHead = NULL;
	pTail = NULL;
	itemCounter = 0;
} /*end queue constructor*/

void queue::addItem(char *pData)
{
	queueItem *pItem = new queueItem(pData, ++itemCounter);

	if(pHead == 0) /*check whether queue has anything in it or not*/
		pHead = pTail = pItem;
	else
	{
		pTail->setNext(pItem);
		pTail = pItem;
	} /*end else statament*/
} /*end addItem function*/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*queueItem.h*/

#ifndef _QUEUEITEM_H_
#define _QUEUEITEM_H_

#include "queue.h"

class queueItem
{
public:
	queueItem(char *pData, int id);
private:
	char data[30];
	int itemId;
	queueItem *pNext;
}; /*end class QueueItem*/

#endif 

1
2
3
4
5
6
7
8
9
10
11
/*queueItem.cpp*/  //#include <cstrng>
#include "queueItem.h"

using namespace std;

queueItem::queueItem(char *pData, int id)
{
	strncpy(data, pData, 29);
	id = itemId;
	pNext = NULL;
}


C:\Users\Loot\Desktop\Program Dump\fifo\main.cpp||In function 'int main()':|
C:\Users\Loot\Desktop\Program Dump\fifo\main.cpp|8|warning: deprecated conversion from string constant to 'char*'|
C:\Users\Loot\Desktop\Program Dump\fifo\main.cpp|9|warning: deprecated conversion from string constant to 'char*'|
C:\Users\Loot\Desktop\Program Dump\fifo\main.cpp|10|warning: deprecated conversion from string constant to 'char*'|
||=== Build finished: 0 errors, 3 warnings ===|


If someone could steer me in the right direction, it would be most appreciated.
In main, you're passing 3 character string literals which are implicitly const char *

Your declaration of queue::addItem says it takes a char *
The compiler is giving you a warning because you're trying to pass a const char * to a char *. Change addItem to take a const char *. You'll need to change queueItem's constructor also.

Awesome, thanks AbstractionAnon! Those errors are taken care of. But now the compiler is taking issue with my queue.h header.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*queue.h*/
#ifndef _QUEUE_H_
#define _QUEUE_H_

#include <iostream>
#include <string>
#include "queueItem.h"

class queue {
public:
	queue();
	void addItem(const char *pData);
private:
	queueItem *pHead;
	queueItem *pTail;
	int itemCounter;
};

#endif 


|||=== fifo, Debug ===|
C:\Users\Loot\Desktop\Program Dump\fifo\queue.h|15|error: ISO C++ forbids declaration of 'queueItem' with no type|
C:\Users\Loot\Desktop\Program Dump\fifo\queue.h|15|error: expected ';' before '*' token|
C:\Users\Loot\Desktop\Program Dump\fifo\queue.h|16|error: ISO C++ forbids declaration of 'queueItem' with no type|
C:\Users\Loot\Desktop\Program Dump\fifo\queue.h|16|error: expected ';' before '*' token|
C:\Users\Loot\Desktop\Program Dump\fifo\queueItem.cpp||In constructor 'queueItem::queueItem(const char*, int)':|
C:\Users\Loot\Desktop\Program Dump\fifo\queueItem.cpp|9|error: 'strncpy' was not declared in this scope|
C:\Users\Loot\Desktop\Program Dump\fifo\queueItem.cpp||In function 'void setNext(queueItem*)':|
C:\Users\Loot\Desktop\Program Dump\fifo\queueItem.cpp|15|error: 'pNext' was not declared in this scope|
||=== Build finished: 6 errors, 0 warnings ===|



It seems to be taking issue with the queueItem pointers.
In queue.h you include queueitem.h where you include queue.h where you include queueitem.h where you include queue.h where you include queueitem.h.... (that's not quite how it works, but if you have files that include each other, it's a problem.)

In queueItem.h you don't need to include queue.h at all.

In queue.h you could get away with a forward declaration of : class queueItem; at file scope.
Last edited on
cire, you're a gentleman and a scholar! I took out the superfluous #include from queueItem.h and the error messages subsided. Thank you!
Topic archived. No new replies allowed.