Could someone check my code as to why my print function is not
working? Also my pop member function is getting an error in main.

template<class Type> 
void Novice<Type>::Print() 
{ 
   while(! IsEmpty()) 
   { 
     std::cout << topPtr->item; 
     topPtr = topPtr ->next; 
   } 

} 


template<class Type> 
void Novice<Type>::Pop(Type &y) 
{ 
     if(IsEmpty()) 
        throw EmptyStack(); 
     else 
     { 
         Node<Type> *tempPtr; 
         tempPtr = topPtr; 
         topPtr = topPtr -> next; 
         delete tempPtr; 
     } 

} 


// main function 

int main() 
{ 
    Novice<int> a; 

    a.Pop();     // not working 
    a.Push(1); 
    a.Push(2); 
    a.Push(3); 

    cout << a.length() << endl; 
    a.Pop(); 
    cout <<  a.length() << endl; 
    a.Print(); 
}

The compilation error: 13 H:no matching function for call to
`Novice<int>::Pop()'

Member Avatar for iamthwee

You are not using code tags properly.

You have a parameter in Novice::Pop that you fail to use within the function, and fail to pass to it in your main() function.

Can you tell me what is the correct way to pass the memory address of the top pointer to that calling function in main? What about the print function?

>Can you tell me what is the correct way to pass the memory
>address of the top pointer to that calling function in main?
Return the value.

Could you perhaps look at the program and check the problem? It fails on execution.

#include <cstdlib>
#include <new>
#include <iostream>
#include ".h"
using namespace std;
int main()
{
Stack<int> IntStack;
int x;
 
IntStack.Pop(x);
IntStack.Push(1);
IntStack.Push(2);
IntStack.Push(3);
 
cout << "int length 1 = " << IntStack.length() << endl;
IntStack.Pop(x);
 
cout << "int length 2 = " << " 2" /*IntStack.size()*/ << endl;
//IntStack.Print(); // I've blocked this out;not working either
 
Stack<float> FloatStack;
float y;
FloatStack.Pop(y);
FloatStack.Push(7.1);
cout << "float length 1 = " << FloatStack.length() << endl;
FloatStack.Push(2.3);
FloatStack.Push(3.1);
cout << "float length 2 = " << FloatStack.length() << endl;
 
FloatStack.Pop(y);
 
//FloatStack.Print();
 
system("PAUSE");
return 0;
}
#include <cstdlib>
#include <new>
class FullStack{};
class EmptyStack{};
template<class ItemType>
struct NodeType;
template<class ItemType>
class Stack
{
private:
NodeType<ItemType>* topPtr; 
public:
Stack(); 
Stack(const Stack<ItemType> &x);
void MakeEmpty(); // stack is made empty
bool IsEmpty(); // test if the stack is empty
bool IsFull(); // test if the stack is full
int length(); 
void Print(); 
void Push(ItemType x); // insert x onto the stack
// precondition: the stack is not full
void Pop(ItemType &x); 
~Stack();
};
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
template<class ItemType>
Stack<ItemType>::Stack()
{
topPtr = NULL;
}
template<class ItemType>
Stack<ItemType>::Stack(const Stack<ItemType> &x)
{
NodeType<ItemType>* ptr1 ;
NodeType<ItemType>* ptr2 ;
if (x.topPtr == NULL )
topPtr = NULL ;
else // allocate memory for first node
{ topPtr = new NodeType<ItemType> ;
topPtr->info = x.topPtr->info ;
ptr1 = x.topPtr->next ;
ptr2 = topPtr ;
while ( ptr1 != NULL ) // deep copy other nodes
{ ptr2->next = new NodeType<ItemType> ;
ptr2 = ptr2->next ;
ptr2->info = ptr1->info ;
ptr1 = ptr1->next ;
}
ptr2->next = NULL ;
}
}
template<class ItemType>
void Stack<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(topPtr != NULL)
{
tempPtr = topPtr;
topPtr = topPtr -> next;
delete tempPtr;
}
 
}
template<class ItemType>
bool Stack<ItemType>::IsEmpty()
{
return (topPtr == NULL); 
}
template<class ItemType>
bool Stack<ItemType>::IsFull()
{
NodeType<ItemType>* loc;
try
{
loc = new NodeType<ItemType>;
delete loc;
return false;
}
catch(std::bad_alloc exception)
{
return true;
} 
}
template<class ItemType>
int Stack<ItemType>::length()
{
int count = 0;
while(topPtr != NULL)
{
count++;
topPtr = topPtr->next;
}
return count;
}
/*template<class ItemType>
void Stack<ItemType>::Print()
{
 
}*/
template<class ItemType>
void Stack<ItemType>::Push(ItemType x)
{
NodeType<ItemType>* location;
if(IsFull())
throw FullStack();
else
{ 
location = new NodeType<ItemType>;
location -> info = x;
location -> next = topPtr;
topPtr = location;
}
} 
template<class ItemType>
void Stack<ItemType>::Pop(ItemType &x)
{
if(IsEmpty())
throw EmptyStack();
else
{
NodeType<ItemType> *tempPtr;
tempPtr = topPtr;
topPtr = topPtr -> next;
delete tempPtr;
 
}
}
template<class ItemType>
Stack<ItemType>::~Stack()
{
MakeEmpty();
}

There's a number of problems/neglections:

In your pop function, you never assign 'x' to the value that was popped off, making the parameter useless. print doesn't work because you've commented both the call and function itself out. Plus there's nothing inside the commented-out function body.

And when you calculate the length of the stack, you use topptr as the iterator variable. Of course, after you reach the end of the stack, you have no way of getting back to the beginning, and all subsequent stack calls fail.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.