PDA

Tam Sürümünü Görmek İçin : stack (hata nerede? )


Lewissi
28/10/2005, 16:20
herkese merhaba,
link list kullanarak stack olusturuyorum.
stack.cpp dosyasini derledigimde sorun cikmiyor ama main.cpp dosyasini derledigimde hata aliyorum.

hata neden oluyor?


// bu stack.cpp dosyasi
#include <iostream>
#include<string>
using namespace std;
class Underflow { };
class Overflow { };
class OutOfMemory { };
class BadIterator { };
// Stack class -- linked list implementation
// CONSTRUCTION: with no parameters
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// Object top( ) --> Return most recently inserted item
// Object topAndPop( ) --> Return and remove most recently inserted item
// bool isEmpty( ) --> Return true if empty; else false
// bool isFull( ) --> Return true if full; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS************************** ******
// Overflow and Underflow thrown as needed
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & rhs );
~Stack( );
bool isEmpty( ) const;
bool isFull( ) const;
const Object & top( ) const;
void pop( );
void makeEmpty(
{while( !isEmpty( ) )
pop();
}
void push( const Object & x );
Object topAndPop( );
const Stack & operator=( const Stack & rhs );
private:
struct ListNode
{
Object element;
ListNode *next;
ListNode( const Object & theElement, ListNode * n = NULL )
: element( theElement ), next( n ) { }
};
ListNode *topOfStack;
};
// /////////////////////////////////////////////////////////////////
template <class Object>
Stack<Object>::Stack( )
{
topOfStack = NULL;
} // Copy constructor.
template <class Object>
Stack<Object>::Stack( const Stack<Object> & rhs )
{
topOfStack = NULL;
*this =rhs;
}
// Destructor
template <class Object>
Stack<Object>::~Stack( )
{
makeEmpty( );
}
/*
Test if the stack is logically full.
Return false always, in this implementation.
*/
template <class Object>
bool Stack<Object>::isFull( ) const
{
return false;
}
// Test if the stack is logically empty.
// Return true if empty, false otherwise.
template <class Object>
bool Stack<Object>::isEmpty( ) const
{
return topOfStack == NULL;
}
// Make the stack logically empty.
/*
template <class Object>
void Stack<Object>::makeEmpty( )
{while( !isEmpty( ) )
pop();
}
*/
/* Get the most recently inserted item in the stack.
Return the most recently inserted item in the stack
or throw an exception if empty.
*/
template <class Object>
const Object & Stack<Object>::top( ) const
{
if( isEmpty( ) )
throw Underflow( );
return topOfStack->element;
}
/* Remove the most recently inserted item from the stack.
Exception Underflow if the stack is empty.
*/
template <class Object>
void Stack<Object>::pop( )
{
if( isEmpty( ) )
ListNode * oldTop = topOfStack;
topOfStack = topOfStack->next; delete oldTop;
}
/* Return and remove the most recently inserted item
from the stack.
*/
template <class Object>
Object Stack<Object>::topAndPop( )
{

Object topItem = top( );
pop( );
return topItem;
}
// Insert x into the stack.
template <class Object>
void Stack<Object>::push( const Object & x )
{
topOfStack = new ListNode( x, topOfStack );
}
// Deep copy.
template <class Object>
const Stack<Object> & Stack<Object>::
operator=( const Stack<Object> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
if( rhs.isEmpty( ) )
return *this;
ListNode *rptr = rhs.topOfStack;
ListNode *ptr = new ListNode( rptr->element );
topOfStack = ptr;
for( rptr = rptr->next; rptr != NULL; rptr = rptr->next )
ptr = ptr->next = new ListNode( rptr->element );
}
return *this;
}


// main dosyasi


#include"stack.cpp"
int main( )
{
Stack<int> s;
Stack<int>s1;
for( int i = 0; i < 10; i++ )
s.push( i );
s1 = s;
cout << "s" << endl;
while( !s.isEmpty( ) )
cout << s.topAndPop( ) << endl;
cout << endl << "s1" << endl;
while( !s1.isEmpty( ) )
cout << s1.topAndPop( ) << endl;
return 0;
}


acehreli
28/10/2005, 19:49
Derleme hatasinin ne oldugunu neden soylemiyorsun?

Ali

Lewissi
28/10/2005, 20:14
g++ da derledigimde su hatalarI alIyorum.

stack.cpp: In member function `void Stack<Object>::pop() [with Object = int]':
stack.cpp:34: instantiated from `void Stack<Object>::makeEmpty() [with Object = int]'
stack.cpp:65: instantiated from `Stack<Object>::~Stack() [with Object = int]'
main.cpp:4: instantiated from here
stack.cpp:110: error: `oldTop' undeclared (first use this function)
stack.cpp:110: error: (Each undeclared identifier is reported only once for each function it appears in.

acemi
28/10/2005, 20:58
void makeEmpty(

->>

void makeEmpty()

Lewissi
28/10/2005, 21:34
o kopyalar iken olmus.bu uyarılar parantez hatasından olmamalı.

acehreli
28/10/2005, 21:45
Hata mesaji, su satirda oldTop'in ne oldugunun bilinmedigini soyluyor (pencerenin sag tarafina da bakin):


topOfStack = topOfStack->next; delete oldTop;


Bunun nedeni, oldTop'in bir if blogunda tanimlanmis olmasi:


if( isEmpty( ) )
ListNode * oldTop = topOfStack;


if blogununu disinda oldTop ismi taninmaz. Cok onerilen ve benim de her zaman uydugum bir kurali uygulayip, blok paratezlerini acikca yazarsak hatayi daha rahat gorebiliriz:


if( isEmpty( ) )
{
ListNode * oldTop = topOfStack;
}

// oldTop'in yasami sona erdi bile...


Ali

Lewissi
28/10/2005, 22:21
tesekkur ederim...