10 Replies - 1165 Views - Last Post: 22 April 2012 - 01:33 PM Rate Topic: -----

#1 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

problem with queueCount function

Posted 22 April 2012 - 10:39 AM

I have this program and its almost done. I just have an issue with the count function, im not entirely sure how to fix my error, but i DO KNOW what it is, i just dont know how to go about fixing it. any hints, comments, or really just about anything on this would be appreciated.


header file
//Header file QueueAsArray

#ifndef _NODE_
#define _NODE_

template <class Type>
struct Node                         // struct for storing some item
{                                   // in a linked list
    Type item;
    Node<Type> * next;
};

#endif

#ifndef H_linkedQueue
#define H_linkedQueue
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class linkedQueueType
{
public:
    const linkedQueueType<Type>& operator=(const linkedQueueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    bool isEmptyQueue();
	void queueCount();

    Type front() const;

    Type back() const;

	void clearFront();

    void addQueue(Type queueElement);
    void deleteQueue();

    linkedQueueType();
    linkedQueueType(const linkedQueueType<Type>& otherQueue); // copy constructor
    ~linkedQueueType(); //destructor

private:
	Node<Type> *lastElem;
    Node<Type> *queueFront;
    Node<Type> *queueRear;
	int count;
	Node<Type> * CopyList(const Node<Type> * ptr) const;
};

template <class Type>
void linkedQueueType<Type>::clearFront()
{
	delete queueFront;
}

template<class Type>
void linkedQueueType<Type>::queueCount()
{
	return count;
}

template<class Type>
void linkedQueueType<Type>::initializeQueue()
{
	count = 0;
    queueFront->next = queueRear;
	queueRear->next = queueFront;
}

template<class Type>
bool linkedQueueType<Type>::isEmptyQueue()
{
	if(count == 0)
	{
		return true;
	}
	return false;
}

template<class Type>
void linkedQueueType<Type>::addQueue(Type newElement)
{
	if(isEmptyQueue())
	{
		queueFront->next = lastElem;
		lastElem->next = queueRear;
		lastElem->item = newElement;
	}
	else
	{
		queueRear->next = new Node( item ); //creating the link
		queueRear = queueRear->next; //updating the last element
	}

	count++;
}

template<class Type>
void linkedQueueType<Type>::deleteQueue()
{
	if(!isEmptyQueue()) 
	{
		Node *currNode = queueFront->next;
		for(int i = 0; i < count; i++)
		{
			currNode->item = 0;
			currNode = currNode->next;
		}
		count = 0;
	}
}

template<class Type>
Type linkedQueueType<Type>::front() const
{
    assert(!isEmptyQueue());
    return (queueFront->next->item);
}

template<class Type>
Type linkedQueueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return (lastElem->item);
}

template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=(const linkedQueueType<Type>& otherQueue)
{
    if (this != &otherQueue) //avoid self-copy
    {
        while(!IsEmpty())
		{
			clearFront();
		}
		if (otherQueue.queueFront == NULL) 
		{
			queueFront = NULL;
			queueRear = NULL;
		}

		else 
		{
			queueFront = CopyList(otherQueue.queueFront);
			queueRear = queueFront;
			while (queueRear->next != NULL) 
			{
				queueRear = queueRear->next;
			}
		}
	}

    return *this;
}

template <class Type>
Node<Type> * linkedQueueType<Type>::CopyList(const Node<Type> * ptr) const
{
    if (ptr == NULL)
	{
        return NULL;
    }
    else {
        Node<Type> * temp = new Node<Type>;
        temp->item = ptr->item;
        temp->next = CopyList(ptr->next);
        return temp;
    }
}

template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
	if(otherQueue.queueFront == NULL)
	{
		queueFront = NULL;
		queueRear = NULL;
	}
	else
	{
		queueFront = CopyList(otherQueue.queueFront);
		queueRear = queueFront;
	}
	while (queueRear->next != NULL) 
	{
	    queueRear = queueRear->next;
	}
}

#endif




main file
#include <iostream>

#include "linkedQueue.h"

using namespace std;

int size = 1000;

int main()
{
	linkedQueueType<int> queue1, queue2;
	int x, y;

	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);
	queue1.addQueue(23);

	cout << "queue1 size is equal to " << queue1.queueCount << endl;

	cout << "queue2 size is equal to " << queue2.queueCount << endl;

	queue2 = queue1;

	cout << "new queue1 size is equal to " << queue1.queueCount << endl;

	cout << "new queue2 size is equal to " << queue2.queueCount << endl;

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl;

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem with queueCount function

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: problem with queueCount function

Posted 22 April 2012 - 10:52 AM

Quote

I just have an issue with the count function, im not entirely sure how to fix my error, but i DO KNOW what it is, i just dont know how to go about fixing it.

What is your issue?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: problem with queueCount function

Posted 22 April 2012 - 10:55 AM

c:\users\soyo\documents\visual studio 2010\projects\project_4\project_4\main.cpp(26): error C3867: 'linkedQueueType<Type>::queueCount': function call missing argument list; use '&linkedQueueType<Type>::queueCount' to create a pointer to member
1> with
1> [
1> Type=int
1> ]


thats the complete error, and from what i have gathered the error is that im trying to return a Type format where as the main program sees it as an int format and therefore it conflicts
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: problem with queueCount function

Posted 22 April 2012 - 11:00 AM

Look closely at the following snippet:
void linkedQueueType<Type>::queueCount()
{
	return count;
}

Do you see anything wrong? What type of variable is count? What is your return type?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: problem with queueCount function

Posted 22 April 2012 - 11:04 AM

You need to call initializeQueue on your classes before you can go using them. This is because in initializeQueue you do crucial things like setting "count" to zero. Until this happens, count is full of garbage from memory.

Secondly, you have queueCount defined as "Void" and yet you try to return an integer in it. Make sure you change it from "Void" to int.

Third, you use queue1.queueCount but it is a method, so you have to add parenthesis! It is queue1.queueCount()

Fourth, you don't have a function called IsEmpty() but you use one in your operator= overload.

Fifth, you have "item" in your addQueue method, but you have not declared an item variable. Perhaps you meant newElement?

That is far as I am going, but you have more errors.
Was This Post Helpful? 0
  • +
  • -

#6 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: problem with queueCount function

Posted 22 April 2012 - 11:04 AM

i believe that he return type is Type but in the cpp file it calls an int, or i could be wrong, but thats how i see it right now
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: problem with queueCount function

Posted 22 April 2012 - 11:05 AM

No your return type is void, can you return a value from a void function?

Jim
Was This Post Helpful? 0
  • +
  • -

#8 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: problem with queueCount function

Posted 22 April 2012 - 11:36 AM

okay i have narrowed it down to a few errors similar to what i had before. these errors are still a bit confusing to me.

error

1>------ Build started: Project: project_4, Configuration: Debug Win32 ------
1>Build started 4/22/2012 2:32:36 PM.
1>InitializeBuildStatus:
1> Touching "Debug\project_4.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>c:\users\soyo\documents\visual studio 2010\projects\project_4\project_4\linkedqueue.h(95): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'Node<Type>'
1> with
1> [
1> Type=int
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> c:\users\soyo\documents\visual studio 2010\projects\project_4\project_4\linkedqueue.h(86) : while compiling class template member function 'void linkedQueueType<Type>::addQueue(Type)'
1> with
1> [
1> Type=int
1> ]
1> c:\users\soyo\documents\visual studio 2010\projects\project_4\project_4\main.cpp(11) : see reference to class template instantiation 'linkedQueueType<Type>' being compiled
1> with
1> [
1> Type=int
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.22
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

header file updated
//Header file QueueAsArray

#ifndef _NODE_
#define _NODE_

template <class Type>
struct Node                         // struct for storing some item
{                                   // in a linked list
    Type item;
    Node<Type> * next;
};

#endif

#ifndef H_linkedQueue
#define H_linkedQueue
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class linkedQueueType
{
public:
    const linkedQueueType<Type>& operator=(const linkedQueueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    bool isEmptyQueue();
	int queueCount();

    Type front();

    Type back() const;

	void clearFront();

    void addQueue(Type queueElement);
    void deleteQueue();

    linkedQueueType();
    linkedQueueType(const linkedQueueType<Type>& otherQueue); // copy constructor
    ~linkedQueueType(); //destructor

private:
	Node<Type> *lastElem;
    Node<Type> *queueFront;
    Node<Type> *queueRear;
	int count;
	Node<Type> * CopyList(const Node<Type> * ptr) const;
};

template <class Type>
void linkedQueueType<Type>::clearFront()
{
	delete queueFront;
}

template<class Type>
int linkedQueueType<Type>::queueCount()
{
	return count;
}

template<class Type>
void linkedQueueType<Type>::initializeQueue()
{
	count = 0;
    queueFront->next = queueRear;
	queueRear->next = queueFront;
}

template<class Type>
bool linkedQueueType<Type>::isEmptyQueue()
{
	if(count == 0)
	{
		return true;
	}
	return false;
}

template<class Type>
void linkedQueueType<Type>::addQueue(Type newElement)
{
	if(isEmptyQueue())
	{
		queueFront->next = lastElem;
		lastElem->next = queueRear;
		lastElem->item = newElement;
	}
	else
	{
		queueRear->next = Node<Type>(newElement); //creating the link
		queueRear = queueRear->next; //updating the last element
	}

	count++;
}

template<class Type>
void linkedQueueType<Type>::deleteQueue()
{
	if(!isEmptyQueue()) 
	{
		Node<Type> *currNode = queueFront->next;
		for(int i = 0; i < count; i++)
		{
			currNode->item = 0;
			currNode = currNode->next;
		}
		count = 0;
	}
}

template<class Type>
Type linkedQueueType<Type>::front()
{
    assert(!isEmptyQueue());
    return (queueFront->next->item);
}

template<class Type>
Type linkedQueueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return (lastElem->item);
}

template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=(const linkedQueueType<Type>& otherQueue)
{
    if (this != &otherQueue) //avoid self-copy
    {
        while(!isEmptyQueue())
		{
			clearFront();
		}
		if (otherQueue.queueFront == NULL) 
		{
			queueFront = NULL;
			queueRear = NULL;
		}

		else 
		{
			queueFront = CopyList(otherQueue.queueFront);
			queueRear = queueFront;
			while (queueRear->next != NULL) 
			{
				queueRear = queueRear->next;
			}
		}
	}

    return *this;
}

template <class Type>
Node<Type> * linkedQueueType<Type>::CopyList(const Node<Type> * ptr) const
{
    if (ptr == NULL)
	{
        return NULL;
    }
    else {
        Node<Type> * temp = new Node<Type>;
        temp->item = ptr->item;
        temp->next = CopyList(ptr->next);
        return temp;
    }
}

template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
	if(otherQueue.queueFront == NULL)
	{
		queueFront = NULL;
		queueRear = NULL;
	}
	else
	{
		queueFront = CopyList(otherQueue.queueFront);
		queueRear = queueFront;
	}
	while (queueRear->next != NULL) 
	{
	    queueRear = queueRear->next;
	}
}

#endif




main cpp updated
#include <iostream>

#include "linkedQueue.h"

using namespace std;

int size = 1000;

int main()
{
	linkedQueueType<int> queue1, queue2;
	int x, y;

	queue1.initializeQueue();
	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);
	queue1.addQueue(23);

	cout << "queue1 size is equal to " << queue1.queueCount() << endl;

	cout << "queue2 size is equal to " << queue2.queueCount() << endl;

	queue2 = queue1;

	cout << "new queue1 size is equal to " << queue1.queueCount() << endl;

	cout << "new queue2 size is equal to " << queue2.queueCount() << endl;

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl;

	return 0;
}



View Postjimblumberg, on 22 April 2012 - 11:05 AM, said:

No your return type is void, can you return a value from a void function?

Jim



i see that now, thank you
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: problem with queueCount function

Posted 22 April 2012 - 11:45 AM

Quote

1>c:\users\soyo\documents\visual studio 2010\projects\project_4\project_4\linkedqueue.h(95): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'Node<Type>'

This should be line 95:
		queueRear->next = Node<Type>(newElement); //creating the link

You do not have a Node<Type> constructor that takes any arguments.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: problem with queueCount function

Posted 22 April 2012 - 12:22 PM

do you make another constructor for Node<Type>? and if so how would you pass the arguments into that constructor?
Was This Post Helpful? 0
  • +
  • -

#11 yotic   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: problem with queueCount function

Posted 22 April 2012 - 01:33 PM

The error is invisible to me. Im not sure why it wont compile? and the error code makes no sense.

error code

1>------ Build started: Project: project_4, Configuration: Debug Win32 ------
1>Build started 4/22/2012 4:28:46 PM.
1>InitializeBuildStatus:
1> Touching "Debug\project_4.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall linkedQueueType<int>::linkedQueueType<int>(void)" (??0?$[email protected]@@[email protected]) referenced in function _main
1>C:\Users\SOYO\Documents\Visual Studio 2010\Projects\project_4\Debug\project_4.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.46
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


header file
#ifndef _NODE_
#define _NODE_

template <class Type>
struct Node                         // struct for storing some item
{                                   // in a linked list
    Type item;
    Node<Type> * next;
};

#endif

#ifndef H_linkedQueue
#define H_linkedQueue
  
#include <iostream>
#include <cassert>
#include <cstddef>
#include <new>

using namespace std;

template <class Type>
class linkedQueueType
{
public:
    const linkedQueueType<Type>& operator=(const linkedQueueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    int isEmptyQueue() const {return ((queueFront) ? 0 : 1);};
	int queueCount();

    Type front() const;

    Type back() const;

    linkedQueueType<Type>& addQueue(const Type & newElement);
    void deleteQueue();

    linkedQueueType();
    linkedQueueType(const linkedQueueType<Type>& otherQueue); // copy constructor
    ~linkedQueueType(); //destructor

private:
	Node<Type> *lastElem;
    Node<Type> *queueFront;
    Node<Type> *queueRear;
	int count;
	Node<Type> * CopyList(const Node<Type> * ptr) const;
};

template <class Type>
linkedQueueType<Type>::~linkedQueueType()
{// Queue destructor.  Delete all nodes.
	Node<Type> *link;
	while (queueFront) {
		link = queueFront->next;
		delete queueFront;
		queueFront = link;
	}
}

template<class Type>
int linkedQueueType<Type>::queueCount()
{
	return count;
}

template<class Type>
void linkedQueueType<Type>::initializeQueue()
{
	count = 0;
    queueFront->next = queueRear;
	queueRear->next = queueFront;
}

template<class Type>
linkedQueueType<Type>& linkedQueueType<Type>::addQueue(const Type & newElement)
{
	Node<Type> *ptr;

	ptr = new Node<Type>;
	ptr->item = newElement;
	ptr->next = NULL;
	if (queueFront)
	{
		queueRear->next = ptr;
	}
	else
	{
		queueFront = ptr;
		queueRear = ptr;
	}

	count++;
	return *this;
}

template<class Type>
void linkedQueueType<Type>::deleteQueue()
{
	if(!isEmptyQueue()) 
	{
		Node<Type> *currNode = queueFront->next;
		for(int i = 0; i < count; i++)
		{
			currNode->item = 0;
			currNode = currNode->next;
		}
		count = 0;
	}
}

template<class Type>
Type linkedQueueType<Type>::front() const
{
    if (isEmptyQueue())   
	{ 
		cout<<"out of bounds";  
		return -1;
	}
	return queueFront->item;
}

template<class Type>
Type linkedQueueType<Type>::back() const
{
    if (isEmptyQueue()) 
	{ 
		cout<<"out of bounds";
		return -1; 
	}
	return queueRear->item;
}

template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=(const linkedQueueType<Type>& otherQueue)
{
    if (this != &otherQueue) //avoid self-copy
    {
		if (otherQueue.queueFront == NULL) 
		{
			queueFront = NULL;
			queueRear = NULL;
		}

		else 
		{
			queueFront = CopyList(otherQueue.queueFront);
			queueRear = queueFront;
			while (queueRear->next != NULL) 
			{
				queueRear = queueRear->next;
			}
		}
	}

    return *this;
}

template <class Type>
Node<Type> * linkedQueueType<Type>::CopyList(const Node<Type> * ptr) const
{
    if (ptr == NULL)
	{
        return NULL;
    }
    else {
        Node<Type> * temp = new Node<Type>;
        temp->item = ptr->item;
        temp->next = CopyList(ptr->next);
        return temp;
    }
}

template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
	if(otherQueue.queueFront == NULL)
	{
		queueFront = NULL;
		queueRear = NULL;
	}
	else
	{
		queueFront = CopyList(otherQueue.queueFront);
		queueRear = queueFront;
	}
	while (queueRear->next != NULL) 
	{
	    queueRear = queueRear->next;
	}
}

#endif



main file
#include <iostream>

#include "linkedQueue.h"

using namespace std;

int size = 1000;

int main()
{
	linkedQueueType<int> queue1, queue2;
	int x, y;

	queue1.initializeQueue();
	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);
	queue1.addQueue(23);

	cout << "queue1 size is equal to " << queue1.queueCount() << endl;

	cout << "queue2 size is equal to " << queue2.queueCount() << endl;

	queue2 = queue1;

	cout << "new queue1 size is equal to " << queue1.queueCount() << endl;

	cout << "new queue2 size is equal to " << queue2.queueCount() << endl;

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1