Hello, I keep getting errors saying "Invalid conversion from pointer to integer and I have tried both of the following operators * after the variable as well as & and nothing seems to work. All input is greatly appreciated. Thank you all for your time.

void Stack::print( )
{

    StackPtr temp;

    tosPtr = temp;

    while(temp != NULL)
    {
        cout << elements  -> temp;
    }
    if (isEmpty( ))
    {
        cout << "Stack is empty" << endl;
    }
    else
    {
        for (int k = tosPtr; k >= 0; k--)
        {
            cout << setw(5) << elements[k];
            if (k == temp*)
                cout << " <- top of stack";
            cout << endl;
        }
        cout << "-----" << endl;
    }
}

Edited 3 Years Ago by johnnydiamond08

if (k == temp*)
This is testing that the object k, which is an int object, is the same as a temp-pointer. It makes no sense. Are you trying to test that the int k is the same value as what the pointer temp is pointing at? That would be
if (k == *temp)
although that would rely on temp being a pointer to an int, but temp appears to be a StackPtr object.

What are you trying to do? You cannot test that an int is the same as a StackPtr, unless you're willing to write the code for that test yourself (which doesn't sound like it would make any sense anyway - what would it even mean to compare an int - that is, a number - with a memory location?)

Edited 3 Years Ago by Moschops

temp is a StackPtr Object and I need to compare the two values. I know how vague this sounds, and I apologize I didn't write the entire code this was just given to me to trouble shoot.

temp is a StackPtr Object and I need to compare the two values.

What does it mean to compare, say, the number 5 with an object of type StackPtr?

I assume that StackPtr is some kind of pointer-to-a-stack object. If I tell you that your pointer-to-a-stack object is equal to the nubmer 17, what does it mean? It makes no sense. I might as well tell you that the number 6 is equal to a box, for example. It just doesn't make any sense, even if that box contains some numbers.

If the stack is a stack of numbers, you could compare the numbers that are on the stack with an int. That would make sense.

Edited 3 Years Ago by Moschops

For line 21 posted above, it's comparing k to the value pointed by temp. If they're both the same int value the program is supposed to display "top of stack". I apologize if this is not what you're asking me, the concept of stack has me lost, and my programming professor is awful at explanations.

For line 21 posted above, it's comparing k to the value pointed by temp.

temp doesn't point to a value.

Show us the code that actually defines the StackPtr object.

Edited 3 Years Ago by Moschops

struct StackNode {

    int info;
    struct StackNode* next;
    stack <int> mystack;



};

typedef StackNode * StackPtr;



class Stack
{
private:

     StackPtr tosPtr;





public:
    Stack(int);         //create a stack to hold specified number of items
    ~Stack( );          //Destructor of the stack
    int  pop( );        //pop top item from stack
    void push(int);     //push new item on stack
    bool isEmpty( );    //returns true if stack empty
    bool isFull( );     //returns ture if stack full
    void print( );      //prints contents of stack

};

temp points to an object of type StackNode.

So *temp is a StackNode object.

A StackNode object contains three things. An int, named info. A pointer to a StackNode, named next. An object of type stack<int> named mystack.

Note that an object of stack<int> is NOT an object of type Stack, which is the next thing defined in the code you gave above. Is it meant to be?

What I say below here refers to what you showed (i.e. that you're using a C++ stack<int> object, and not this Stack object).

The object named mystack is the actual stack. This is how you get hold of that stack:

*temp.mystack or temp->mystack

mystack is a standard C++ stack. You cannot read its values directly. All you can do is pop a value off the top, push a value onto the top, and check what the value at the top is without popping it off the top. You can also check if it's empty, and check how big it is.

Edited 3 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.