0

Here is the header file code, stack.h:

#include <iostream>
using namespace std;
//template <class T> struct stackNode;
//template <class T>
struct stackNode {
int item;
stackNode *Next;
stackNode *Prev;
//stackNode *Temp;

};
class stackClass {
public:
stackClass();
stackClass(const stackClass &right);
~stackClass();
stackClass &operator=(const stackClass &right);
int counter;
int StackIsEmpty(void);
//stackNode *origptr;
void push( int item, bool &success);
int pop(void);

int GetStackTop(void);

protected:
stackNode *GetStackTopPtr(void);


private:

stackNode *Top;
stackNode *Current;
//stackNode *origptr;
//stackNode<T> *Next; 
};

Here is the implementation file, stack.cpp:

#include "stack.h"

//constructor 
stackClass::stackClass(){
        Top = NULL;
        int counter = 0;
            }   

//copy constructor
stackClass::stackClass(const stackClass &right){
        if (right.Top == NULL){
        Top = NULL;
        counter = 0;
            }
        else {
            stackNode * origptr = right.GetStackTopPtr; 
        stackNode * currptr;
        Top = new stackNode; 
        currptr = Top;

        while(origptr->Next != NULL){
            currptr->item = origptr->item;
            origptr = origptr->Next;
            currptr->Next = new stackNode;
            currptr->Next->item = origptr->item;
            currptr = currptr-> Next;
            }
        currptr->Next = NULL;
            }   
    }

//Destructor
stackClass::~stackClass(){  
    while (Top != NULL){
        pop();
                    }               
            }
//push
void stackClass::push(int item, bool &success){
    success = false;
    if (Top == NULL){
        Top = new stackNode;
        Top -> item = item;
        Top -> Next = NULL;
        Top -> Prev = NULL;
        Current = Top; 
        counter++;
        success = true;
        }
    else {
        stackNode * Temp = new stackNode;
        Current -> Next = Temp;
        Temp -> Prev = Current;
        Temp -> Next = NULL; 
        Current = Temp;
        Current -> item = item;
        counter++;
        success = true;
        }

    }

int stackClass::pop(void){      

    if (Top == NULL){
        cout<<"Stack is empty"<<endl;
        }   

    else if (counter == 1){
        Top = NULL;
        delete Current; 
        counter--;
        }

    else if(counter > 1){
        Current -> Prev -> Next = NULL;
        Current = Current -> Prev;
        stackNode * Temp = Current -> Next;
        delete Temp; 
        counter--;              
        }   
    }   

int stackClass::StackIsEmpty(void){
    if (counter == 0)
        return 1;
    else 
        return 0;
    }

int stackClass::GetStackTop(void){

    int item = Top->item ; 
    return item;    
            }   

stackNode *stackClass::GetStackTopPtr(void){
    return Top;         
            }   

and here is the issue,

stack.cpp: In copy constructor ‘stackClass::stackClass(const stackClass&)’:
stack.cpp:19:31: error: cannot convert ‘stackClass::GetStackTopPtr’ from type ‘stackNode* (stackClass::)()’ to type ‘stackNode*’

I have no idea why the compiler complains, it appears that in the line in question, in stack.cpp, line 19, right.GetStackTopPrt would return a stackNode * type, which should assign nicely to the stackNode* on the left side.

Please help if you know what the issue is. Thanks!

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80

1 Answers1

5

You're trying to set

stackNode * origptr = right.GetStackTopPtr;

where the left hand side is a stackNode pointer, and the right hand side is the function GetStackTopPtr. I think you meant to write:

stackNode * origptr = right.GetStackTopPtr(); // Note the parentheses!

Remember that the compiler is very good at figuring out types, and is almost always right about these things. Don't assume the compiler is wrong, look for a mistake that you made first!

Xymostech
  • 9,710
  • 3
  • 34
  • 44
  • Thanks.I did not really complained that the compiler is complaining, I just said that it does that. – radashevskiy Mar 30 '13 at 20:45
  • @radashevskiy Ah, fair enough. I will agree that there is an art to reading compiler error messages. Did this solve the problem? – Xymostech Mar 30 '13 at 21:51
  • It certainly was a big oversight, and thanks for pointing that out. However, after I fixed that, there was a problem in that line still, so I had to add the word const to end of stackNode *GetStackTopPtr(void) in both header and implementation file. Also had to fix the pop() to return values of the popped node. And still a lot of work to do! – radashevskiy Mar 30 '13 at 23:50