user12051013
user12051013

Reputation:

Why am I getting "invalid conversion from 'Queue*/Stack*' to 'int'" error message?

Why am I getting these errors?

invalid conversion from 'Queue*' to 'int'

conversion from 'Stack*' to non-scalar type 'Stack' requested

I've tried modifying Queue & Stack, but to no avail. I am doing an assignment that implements Stack using Queues & implements Queue using Stacks.

Stack.h

#ifndef STACK_H_
#define STACK_H_
#include <iostream>
using namespace std;

class Stack {
    int size;
    int capacity; // for dynamic allocated array
    int stackTop;
    int *arr;

public:
    Stack();
    void push(int val);
    int pop();
    bool isFull();
    bool empty();
    int top();
    int peek(int pos);
    int resize();
};

bool Stack::empty(){
    return size == 0;
}

bool Stack::isFull(){
    return size == capacity;
}

void Stack::push(int val){

    if(isFull())
        resize();

    arr[++stackTop] = val;
    size++;
}

int Stack::pop(){

    if(empty())
        return true;

    return arr[stackTop--];

}

int Stack::peek(int pos){

    if(pos > stackTop || pos < 0){
        cout << "Empty Stack";
        return 0;
    }
    else{
        return arr[size - pos - 1];
    }
}

int Stack::top(){

    if(empty()){
        return true;
    }

    return *arr;

}

int Stack::resize(){

    return size;
}

Queue.h

#ifndef QUEUE_H_
#define QUEUE_H_
#include <iostream>
using namespace std;

class Queue{

    int f, r, *arr, size, capacity;

public:

    Queue(): f(-1), r(-1), arr(nullptr), size(0), capacity(0){}
    Queue(int cap): f(-1), r(-1), arr(new int[cap]), size(0), capacity(cap){}
    ~Queue(){delete []arr;}

    Queue(const Queue &copy){

        f = copy.f;
        r = copy.r;
        arr = copy.arr;
        size = copy.size;
        capacity = copy.capacity;
    }

    Queue(Queue&& move){

        f = move.f;
        r = move.r;
        arr = move.arr;
        size = move.size;
        capacity = move.capacity;

        move.f = -1;
        move.r = -1;
        move.arr = nullptr;
        move.size = 0;
        move.capacity = 0;
    }

    Queue& operator=(const Queue& copyA){

        if(this == &copyA){

            return *this;
        }

        f = copyA.f;
        r = copyA.r;
        arr = copyA.arr;
        size = copyA.size;
        capacity = copyA.capacity;
    }

    Queue& operator=(const Queue&& moveA){

        if(this == &moveA){

            return *this;
        }

        f = moveA.f;
        r = moveA.r;
        arr = moveA.arr;
        size = moveA.size;
        capacity = moveA.capacity;

//      moveA.f = -1;
//      moveA.r = -1;
//      moveA.arr = nullptr;
//      moveA.size = 0;
//      moveA.capacity = 0;

        return *this;
    }

    void enqueue(int x){
        if(!full())
            resize();
        arr[f + r] = x;
        size++;
    }


    int dequeue(){
        if(!empty()){
            return arr[++f];
        } return -99999;
    }

    bool empty(){
        return size == 0;
    }

    bool full(){
        return size == capacity;
    }

    int peek(int pos){

        if(pos > capacity || pos < 0){
            cout << "Empty Queue";
            return 0;
        }else{
            return arr[size - pos - 1];
        }
    }

    void resize(){
        int newSize = this->size * 2;
        Queue *temp = new Queue[newSize];
        int count = 0;

        for(int i = 0; i < count; ++i){
            int index = (f + 1) % size;
            temp[i] = arr[index];
        }
    }
};

main.cpp

#include <iostream>
#include "Queue.h"
#include "Stack.h"
using namespace std;

int main(){

    Queue q = new Queue(); //invalid conversion from 'Queue*' to 'int' [-fpermissive]

    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);

    cout << q.dequeue() << '\n';
    cout << q.dequeue() << '\n';
    cout << q.dequeue() << '\n';

    cout << endl;

    Stack s = new Stack(); //conversion from 'Stack*' to non-scalar type 'Stack' requested
    s.push(1);
    s.push(2);
    s.push(3);

    cout << "current size: " << s.resize() << endl;
    cout << s.top() << endl;

    s.pop();

    cout << s.top() << endl;

    s.pop();

    cout << s.top() << endl;

    cout << "current size: " << s.resize() << endl;


    return 0;
}
    
main.cpp:8:12: error: invalid conversion from 'Queue*' to 'int' [-fpermissive]
  Queue q = new Queue();
            ^~~~~~~~~~~

20:12: error: conversion from 'Stack*' to non-scalar type 'Stack' requested
  Stack s = new Stack();
            ^~~~~~~~~~~

Upvotes: 0

Views: 631

Answers (3)

acraig5075
acraig5075

Reputation: 10756

I don't agree with the answers advising getting it to work with new. It's unnecessary.

Instead of using new just leave them as normal stack variables. Then you're not burdened with having to later delete and you don't have to replace all instances of . to ->.

Simply

Queue q;
Stack s;

And your program remains otherwise unchanged.

(Do you perhaps come from a background? new is necessary there, but most often not necessary with C++)

Upvotes: 0

iiKop47
iiKop47

Reputation: 156

The error would come from the line in main.cpp:

Queue q = new Queue();

The new keyword creates a pointer to the class object, so the correct syntax would be:

Queue *q = new Queue();

This is also shown in the C++ tutorial documentation here: http://www.cplusplus.com/doc/tutorial/classes/#pointers_to_classes

Same thing for the Stack pointer variable.

Note that this also means the syntax for using the objects must also change.

Instead of:

s.pop();

You will need to modify this to either:

(*s).pop();

or

s->pop();

Hope this helps!

Upvotes: 2

Melanie Kneisel
Melanie Kneisel

Reputation: 109

When you instantiate with the new keyword, you create a pointer to the object. Thus, the correct instantiation would be the following:

Queue * q = new Queue();

Stack * s = new Stack();

When x is a pointer, then the value of x is the address of an object and *x is the actual object.

Upvotes: 1

Related Questions