GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Sep-2007, 05:11
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point
Thumbs up

Operator>> Problem


Hello all expert C++ programmer, i truly new to C++ environment.

I have a program where define the overload version of operator>> but give me some errors.

I know that overload >> version only can ask input for variable inside the class only but i going to cin for local variable. Then the compiler give me some errors. I try to uncomment it but i doesn't compile successful.

Below is my program.

CPP / C++ / C Code:
#pragma once

#include<ostream>
#include<istream>

using std::ostream;
using std::ostream;

template<typename T>
class queue
{
	queue<T> *userQueue;   // Number in the queue
	int numAddBehind; // ++Number in the Behind
	int numDelFront;  // --Number in the Front
	int size;         // Size of the queue
	friend ostream& operator<<(ostream&, const queue<T>&);
	friend istream& operator>>(istream&, queue<T>&);
public:
	queue();
	queue(int);
	queue(const queue&);
	
	void setSize();

	void add(int); // Enqueue
	void del(); // Dequeue
	void display();
	
	int queueSize(); // Check the size of the queue
	queue<T> back(); // Return the last element of the queue
	queue<T> front(); // Return the first element of the queue
	queue<T> random(); // Return random element value specified by user.

	bool isFull();
	bool isEmpty();

	~queue();
};

/* 
1. Read string from user or file
2. Push in to the stack and queue

1. Pop the top of the stack
2. Pop the front of the queue
3. Compare the two chars
4. If they are equal, they are palindrome
*/

/* A Member Function Template 
   may not be Virtual

   template <class T> struct A 
   {
     template <class C> 
	 virtual void f(C);   
     error, virtual template function 
     virtual void g(); //  OK, g is not a template
   };
*/



CPP / C++ / C Code:
#include<iostream>
#include "Queue.h"

using namespace std;

int main(int argc, char *argv[])
{
	queue<int> first;
	queue<int> second(first);

	while(!first.isFull())
	{
		first.add(10);
	}
	cout << "\nSize of the Queue : "
		<< first.queueSize() << "\n";
	first.display();

	cout << "\n\nThe last element of the queue is " 
		<< first.back() << "\n";
	
	cout << "\nThe First element of the queue is "
		<< first.front() << "\n";
	
	cout <<"\nThe random element of the queue is "
		<< first.random() << "\n";

	return 0;
}
// -----------------------------------------
template<typename T>
queue<T>::queue() : numAddBehind(-1), numDelFront(-1)
{
	cout << "How many the number of queue : ";
	cin >> size;

	setSize();
}
// -----------------------------------------
template<typename T>
queue<T>::queue(int userSize) 
: size(userSize), numAddBehind(-1), numDelFront(-1)
{
	setSize();
}
// -----------------------------------------
template<typename T>
queue<T>::queue(const queue& rhs)
{
	this->size = rhs.size;
	this->numAddBehind = rhs.numAddBehind;
	this->numDelFront = rhs.numDelFront;
	try
	{
		userQueue = new queue<T>[size];
	}
	catch(std::bad_alloc& rhs)
	{
		rhs.what();
	}
	for (int loop=0;loop<size;++loop)
	{
		this->userQueue[loop] = rhs.userQueue[loop];
	}
}
// -----------------------------------------
template<typename T>
queue<T>::~queue()
{
	delete [] userQueue;
}
// -----------------------------------------
template<typename T>
void queue<T>::setSize()
{
	try
	{
		userQueue = new queue<T>[size];
	}
	catch(std::bad_alloc& rhs)
	{
		rhs.what();
	}
	for (int loop=0;loop<size;++loop)
	{
		userQueue[loop] = 0;
	}
}
// -----------------------------------------
template<typename T>
void queue<T>::add(int number)
{
	if (!isFull())
	{
		++numAddBehind;
		++numDelFront;
		
		cout << "The queue[" << numAddBehind
			<< "] : ";

		userQueue[numAddBehind] = number;
	}
	else
	{
		cout << "Queue is Full!!!";
		--numAddBehind;
	}
}

// -----------------------------------------
template<typename T>
void queue<T>::del()
{
	if (!isEmpty())
	{
		for (int loop=0;loop<=size;++loop)
		{
			if (loop<=numAddBehind)
			{
				int temp;
				temp = userQueue[loop+1];
				userQueue[loop] = temp;
			}
			else
			{
				numAddBehind--;
				if (numAddBehind == -1)
				{
					numDelFront = -1;
				}
				else
				{
					numDelFront = 0;
				}// Located at first place
			}
		}
	}
	else
	{
		cout << "Queue is empty!!!";
	}

}

// -----------------------------------------
template<typename T>
void queue<T>::display()
{
	if (!isEmpty())
	{
		for (int loop=0;loop<size;++loop)
		{
			cout << "\nThe queue [" << loop
				<< "] : " << userQueue[loop];
		}
	}
}
// -----------------------------------------
template<typename T>
int queue<T>::queueSize()
{
	return size;
}
// -----------------------------------------
template<typename T>
queue<T> queue<T>::back()
{
	return *(userQueue + numAddBehind);
	// Pointer Notation 
}
// -----------------------------------------
template<typename T>
queue<T> queue<T>::front()
{
	int temp = size - 1;
	int frontPosition = numDelFront - temp;

	return *(userQueue + frontPosition);
}
// -----------------------------------------
template<typename T>
queue<T> queue<T>::random()
{
//	int random;
	cout << "Random Access Position : ";
//	cin >> random;

	int temp = random - 1;
    return *(userQueue + temp);
}
// -----------------------------------------
template<typename T>
bool queue<T>::isFull() // Test whether the queue is full
{
	if (numAddBehind == size-1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
// -----------------------------------------
template<typename T>
bool queue<T>::isEmpty() // Test whether the queue is empty
{
	if (numAddBehind == -1 && numDelFront == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
// -----------------------------------------
template<typename T>
ostream& operator<<(ostream& output, const queue<T>& rhs)
{
	output << rhs.userQueue << rhs.numAddBehind
		<< rhs.numDelFront << rhs.size;
	return output;
}
// ------------------------------------------
template<typename T>
istream& operator>>(istream& input, queue<T>& rhs)
{
	input >> rhs.numAddBehind >> rhs.numDelFront
		>> rhs.size;
	return input;
}
// ------------------------------------------



I really need to solve out this problem. Therefore, i hope you can guide me or give some advice.

Thanks for your help.

Your help is greatly appreciated by me and others.
  #2  
Old 13-Sep-2007, 08:03
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 104
dlp will become famous soon enough

Re: Operator>> Problem


What's the error? My theory is it (if only one) says something about not knowing what an istream is. Take a look and see where you tell your class queue where to find the istream definition in the std namespace.
  #3  
Old 13-Sep-2007, 13:25
davis
 
Posts: n/a

Re: Operator>> Problem


Quote:
Originally Posted by Peter_APIIT
I have a program where define the overload version of operator>> but give me some errors.

I didn't spend anytime looking at your code, but maybe this will help? It shows both streaming operator implementations, but for a rather trivial class implementation.

www.gidforums.com


It may be helpful.


:davis:
  #4  
Old 14-Sep-2007, 21:45
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point

Re: Operator>> Problem


I have check the program but the program it does the same thing as me. I mean the operator>> and its definition.

The only difference is i use operator>> to cin local variable. I don't know this will give me errors like this.

Thanks for your help.
  #5  
Old 17-Sep-2007, 11:04
smarvast smarvast is offline
New Member
 
Join Date: Sep 2007
Posts: 2
smarvast is on a distinguished road

Re: Operator>> Problem


Did you try
CPP / C++ / C Code:
using namespace std;
instead of 
using std::ostream;
using std::ostream; // TYPO HERE (should be istream?)

I suspect you are not getting the correct istream library loaded.
Last edited by LuciWiz : 17-Sep-2007 at 13:42.
  #6  
Old 19-Sep-2007, 23:26
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point
Thumbs up

Re: Operator>> Problem


What a blind mistake made by me ?

Thanks for your help.
  #7  
Old 21-Sep-2007, 20:52
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point

Re: Operator>> Problem


I get another error message during execute.

The error message is unresolved external and i know it is related to the definition of a function. You call the function but don't have the implementation.

I really don't know which one i left out.

Error Message:
Quote:
Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class queue<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ std@@AAV01@ABV?$queue@H@@@Z) referenced in function "public: void __thiscall queue<int>::display(void)" (?display@?$queue@H@@QAEXXZ) Queue.obj
Error 2 fatal error LNK1120: 1 unresolved externals D:\C++\Queue\Debug\Queue.exe 1



Sorry for my stupidity.
  #8  
Old 22-Sep-2007, 08:41
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 104
dlp will become famous soon enough

Re: Operator>> Problem


Using your class code, what should happen when we do the following:

CPP / C++ / C Code:
/* include libraries, declare int main, etc. */

    queue<int> q;
    q.add( 5 );
    cout << q << endl;
/* end main, etc. */

q should be displayed but by what method? What are the exact steps as it enters the operator<< definition you've created? I'll give you a hint: The problem lies with this line of code:
CPP / C++ / C Code:
template<typename T>
class queue
{
    queue<T> *userQueue;   // <---- HERE'S THE PROBLEM
  #9  
Old 22-Sep-2007, 20:21
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point

Re: Operator>> Problem


q is display with this the operator<<. I not really sure what is the step the called get into the function(method) definition.

I guess is something like this:
cout.operator<<(q);

After the hind you giving to me, i think i some related to loop right where i inside the queue class, i have create a certain size of queue by i never display it by loop.

I really don't know how to solve after this.

Thanks for your hind and help.
  #10  
Old 24-Sep-2007, 07:18
smarvast smarvast is offline
New Member
 
Join Date: Sep 2007
Posts: 2
smarvast is on a distinguished road

Re: Operator>> Problem


try removing the const keyword from queue in your operator<< overload.
There are other potential problems in your code.
Did you really want a specialised add method just for integers when your queue is of type T??
Peter in order to get better feedback, in general I would recommend you first implement a simple queue with display before making your class templated.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Buffer problem with CD-RWriter KieranC Computer Software Forum - Windows 2 07-Jan-2006 01:45
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.