Aug 27, 2009 at 6:41pm UTC
hello , i am new in c++ and i 'm strive to write a simple template stack to learn templates and stacks ...
i 'm using visual studio C++ express 2008 to write it . the compiler doesn't give me any error but when i run the the program i get an expression _block_type_is_valid phead- nblockuse .
this is my source code
PS : sorry for my bad english . and thanks for your help . if you see any other problem plz tell it :)
--------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
// -----------------------------this is the test.cpp-------------
#include<iostream>
#include"DStack.cpp"
using namespace std;
void main()
{
int n ,i ,item ;
DStack<int > inter;
cin >> n;
for ( i=0; i< n; i++ )
{
cin >> item;
inter.Push(item);
}
}
//--------------Dstack.h--------------------------
//------------------------------------------
#ifndef D_DSTACK_H
#define D_DSTACK_H
//----------------------------------------
template < class T>
class DStack
{
protected :
class DStackNode
{
public :
// This is the strucure of a node.
T Item;
DStackNode *Next;
DStackNode()
{
Next = NULL;
}
DStackNode(const T &TheItem)
{
Item = TheItem;
Next = NULL;
}
DStackNode(const T &TheItem, const DStackNode *NextNode)
{
Item = TheItem;
Next = NextNode;
}
};
DStackNode *StackTop;
void Deallocate();
private :
public :
DStack(); // This Is Our Constructor
DStack(const DStack<T> &AStack);// ThisIsOurCopyConstructor
~DStack(); //This Is Our Destructor
//FunctionOfDStack
bool IsEmpty()const { return ( StackTop == 0 ); }
void Push(const T &x);
bool Pop(T *TopItem);
T Pop( T &x);
bool Top( T *TopItem);
T& Top() const ;
DStack<T> operator =(const DStack<T> &S);
int StackCounter();
};
#endif
//------------------------------Dstack.cpp---------------------
//----------------------------------
#include<iostream>
#include"Dstack.h"
using namespace std;
//----------------------------------
//--------SODeallocation------------
template <class T >
void DStack<T>::Deallocate()
{
DStackNode *ThisPtr, *NextPtr;
//Positioning ThisPtr @ Top
ThisPtr= StackTop;
while ( ThisPtr != 0 )
{
NextPtr=ThisPtr->Next;
delete ThisPtr;
ThisPtr = NextPtr ;
}
}
//---------EODeallocation-------------
//---------SOCunstractor-------------
template <class T>
DStack<T>::DStack()
{
StackTop=0; // initialize
}
//---------EOCunstractor--------------
//---------SOCopyConstractor----------
template <class T>
DStack<T>::DStack(const DStack<T> &AStack)
{
StackNode *Clone ,*Original;
Original = AStack.Top;
if (Original == NULL) {
TopNode = NULL;
} else {
TopNode = Clone = new StackNode(Original ->Item);
while (Original ->Next != NULL){
Original = Original->Next;
Clone ->Next = new StackNode(Original ->Item);
Clone = Clone ->Next ;
}
}
}
//----------EOCopyConstrator-------------
//----------SODestructor----------------
template < class T >
DStack<T>::~DStack()
{
Deallocate();
}
//------------EODestructor----------------
//------------SOPushFunction--------------
template < class T >
void DStack<T>::Push(const T &x )
{
DStackNode *NewNode, *Temp;
NewNode = new DStackNode;
NewNode->Item = x;
if (IsEmpty())
{
StackTop = NewNode;
NewNode->Next = 0;
} else {
NewNode->Next = StackTop;
Temp = NewNode;
delete StackTop;
StackTop = Temp;
}
}
//-----------EOPushFunction----------------------
//--------------SOBoolPop---------------
template <class T>
bool DStack< T >::Pop(T *TopItem)
{
DStackNode *ptr = NULL;
if (!StackTop)
{
return false ;
} else {
ptr = StackTop;
StackTop = StackTop->Next;
TopItem = ptr->Item;
delete ptr;
return true ;
}
}
//------------EOBoolPop------------------
//-----------SOPopFunction--------------
template < class T>
T DStack< T >::Pop( T &x )
{ DStackNode *Temp;
if ( IsEmpty())
{
cout<<"the stack is empty" <<endl<<endl;
} else {
x = StackTop->Item;
Temp = StackTop->Next;
delete StackTop;
StackTop = Temp;
}
}
//-----------EOPopFunction---------------
//----------SOTopFunction( *TopItem)------------------------
bool DStack< T >::Top(T *TopItem)
{
DStackNode *ptr = NULL;
if (!StackTop)
{
return false ;
} else {
return true ;
}
}
//----------EOTopFunction( *TopItem)----------------------
//---------SOItemTopExtraction( T TOP)------
template < class T>
T& DStack< T >::Top() const
{
if ( IsEmpty())
{
cout<<"the stack is empty" <<endl<<endl;
} else {
return StackTop;
}
}
//--------------EOItemTopExtraction(T TOP)---------------------------------
//------------SOoverLoad = -------------------------------------------------
/*
template<class T>
DStack<T>::operator =(const DStack<T> &X)
{
DStackNode *Original,*NewTop ,*NewCopy, X_Node;
X_Node = X.Top;
if(X_Node == NULL){
NewTop = NULL;
} else {
NewTop = new DStackNode(X_Node->Item);
NewCopy = NewTop;
while( Original->Next != NULL;){
Original = Original->Next;
NewCopy->Next = new DStackNode(Original->Item);
NewCopy = NewCopy->Item;
}
}
while(!IsEmpty()){
//pop();
}
//StackTop=NewTop;
}
//------------EOoverLoad = -----------------------------------------
*/
//---------SOStackCounsterFunction----------------------------------------------
template < class T>
int DStack<T>::StackCounter()
{
int Counter=0;
DStackNode *ThisPtr, *NextPtr;
while ( ThisPtr != 0 )
{
NextPtr=ThisPtr->Next;
Counter++;
ThisPtr = Next ;
}
return Counter;
}
//---------EOStackCounterFunction-------------------------------------
thanks for your time :)
Last edited on Aug 27, 2009 at 7:37pm UTC
Aug 27, 2009 at 10:01pm UTC
thanks guestgulkan i think tha the correct is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
template < class T >
void DStack<T>::Push(const T &x )
{
DStackNode *NewNode, *Temp;
NewNode = new DStackNode;
NewNode->Item = x;
if (IsEmpty())
{
StackTop = NewNode;
NewNode->Next = 0;
} else {
NewNode->Next = StackTop;
StackTop = NewNode;
}
}
but i still i 'm getting the expression _block_type_is_valid phead- nblockuse
Last edited on Aug 27, 2009 at 10:05pm UTC
Aug 27, 2009 at 10:09pm UTC
I never got further than that - as I came across that problem straight away.
Aug 27, 2009 at 11:01pm UTC
There are a number of errors in the code you posted which will take some time to go through.
Aug 28, 2009 at 8:36am UTC
could you plz tell me where you see these errors , so i can try to correct them as the above :) .
thanks for your time you spend .
Aug 28, 2009 at 8:37am UTC
Donot seperate the template code into cpp and header file. Compilers are not mature enough to handle that. I tried your example using gcc compiler. I could run the program to successfull completion:
#include<iostream>
#include "DStack.h"
using namespace std;
int main()
{
int n ,i ,item ;
DStack<int> inter;
cin >> n;
for( i=0; i< n; i++ )
{
cin >> item;
inter.Push(item);
}
return 0;
}
//--------------Dstack.h--------------------------
//------------------------------------------
#ifndef D_DSTACK_H
#define D_DSTACK_H
using namespace std;
//----------------------------------------
template< class T>
class DStack
{
protected:
class DStackNode
{
public:
// This is the strucure of a node.
T Item;
DStackNode *Next;
DStackNode()
{
Next = NULL;
}
DStackNode(const T &TheItem)
{
Item = TheItem;
Next = NULL;
}
DStackNode(const T &TheItem, const DStackNode *NextNode)
{
Item = TheItem;
Next = NextNode;
}
};
DStackNode *StackTop;
void Deallocate();
private:
public:
DStack(); // This Is Our Constructor
DStack(const DStack<T> &AStack);// ThisIsOurCopyConstructor
~DStack(); //This Is Our Destructor
//FunctionOfDStack
bool IsEmpty()const { return( StackTop == 0 ); }
void Push(const T &x);
bool Pop(T *TopItem);
T Pop( T &x);
bool Top( T *TopItem);
T& Top() const;
DStack<T> operator=(const DStack<T> &S);
int StackCounter();
};
//--------SODeallocation------------
template<class T >
void DStack<T>::Deallocate()
{
DStackNode *ThisPtr, *NextPtr;
//Positioning ThisPtr @ Top
ThisPtr= StackTop;
while( ThisPtr != 0 )
{
NextPtr=ThisPtr->Next;
delete ThisPtr;
ThisPtr = NextPtr ;
}
}
//---------EODeallocation-------------
//---------SOCunstractor-------------
template<class T>
DStack<T>::DStack()
{
StackTop=0; // initialize
}
//---------EOCunstractor--------------
//---------SOCopyConstractor----------
template<class T>
DStack<T>::DStack(const DStack<T> &AStack)
{
DStackNode *Clone ,*Original, *TopNode;
Original = AStack.Top;
if (Original == NULL) {
TopNode = NULL;
} else {
TopNode = Clone = new DStackNode(Original ->Item);
while (Original ->Next != NULL){
Original = Original->Next;
Clone ->Next = new DStackNode(Original ->Item);
Clone = Clone ->Next ;
}
}
}
//----------EOCopyConstrator-------------
//----------SODestructor----------------
template< class T >
DStack<T>::~DStack()
{
Deallocate();
}
//------------EODestructor----------------
//------------SOPushFunction--------------
template< class T >
void DStack<T>::Push(const T &x )
{
DStackNode *NewNode, *Temp;
NewNode = new DStackNode;
NewNode->Item = x;
if (IsEmpty())
{
StackTop = NewNode;
NewNode->Next = 0;
} else {
NewNode->Next = StackTop;
Temp = NewNode;
delete StackTop;
StackTop = Temp;
}
}
//-----------EOPushFunction----------------------
//--------------SOBoolPop---------------
template<class T>
bool DStack< T >::Pop(T *TopItem)
{
DStackNode *ptr = NULL;
if(!StackTop)
{
return false;
} else {
ptr = StackTop;
StackTop = StackTop->Next;
TopItem = ptr->Item;
delete ptr;
return true;
}
}
//------------EOBoolPop------------------
//-----------SOPopFunction--------------
template< class T>
T DStack< T >::Pop( T &x )
{ DStackNode *Temp;
if ( IsEmpty())
{
cout<<"the stack is empty"<<endl<<endl;
} else {
x = StackTop->Item;
Temp = StackTop->Next;
delete StackTop;
StackTop = Temp;
}
}
//-----------EOPopFunction---------------
//----------SOTopFunction( *TopItem)------------------------
template< class T>
bool DStack< T >::Top(T *TopItem)
{
DStackNode *ptr = NULL;
if(!StackTop)
{
return false;
} else {
return true ;
}
}
//----------EOTopFunction( *TopItem)----------------------
//---------SOItemTopExtraction( T TOP)------
template< class T>
T& DStack< T >::Top() const
{
if ( IsEmpty())
{
cout<<"the stack is empty"<<endl<<endl;
} else {
return StackTop;
}
}
//--------------EOItemTopExtraction(T TOP)---------------------------------
//------------SOoverLoad = -------------------------------------------------
/*
template<class T>
DStack<T>::operator =(const DStack<T> &X)
{
DStackNode *Original,*NewTop ,*NewCopy, X_Node;
X_Node = X.Top;
if(X_Node == NULL){
NewTop = NULL;
} else {
NewTop = new DStackNode(X_Node->Item);
NewCopy = NewTop;
while( Original->Next != NULL;){
Original = Original->Next;
NewCopy->Next = new DStackNode(Original->Item);
NewCopy = NewCopy->Item;
}
}
while(!IsEmpty()){
//pop();
}
//StackTop=NewTop;
}
//------------EOoverLoad = -----------------------------------------
*/
//---------SOStackCounsterFunction----------------------------------------------
template< class T>
int DStack<T>::StackCounter()
{
int Counter=0;
DStackNode *ThisPtr, *NextPtr;
while( ThisPtr != 0 )
{
NextPtr=ThisPtr->Next;
Counter++;
ThisPtr = NextPtr ;
}
return Counter;
}
//---------EOStackCounterFunction-------------------------------------
#endif
Aug 28, 2009 at 1:00pm UTC
thanks klkvgupta ,i put the Dstack.h and theDstack.cpp in one file and i run the program successfull . that was the problem i didn't get any expression now the only thing that matter is my code is correct.