【问题标题】:Why am I getting "invalid conversion from 'Queue*/Stack*' to 'int'" error message?为什么我会收到“从 'Queue*/Stack*' 到 'int' 的无效转换”错误消息?
【发布时间】:2019-10-07 23:21:58
【问题描述】:

为什么会出现这些错误?

从 'Queue*' 到 'int' 的无效转换

请求从“堆栈*”转换为非标量类型“堆栈”

我尝试修改Queue & Stack,但无济于事。我正在做一个使用Queues 实现Stack 并使用Stacks 实现Queue 的作业。

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];
        }
    }
};

ma​​in.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:错误:从 'Queue*' 到 'int' 的无效转换 [-fpermissive] 队列 q = new Queue(); ^~~~~~~~~~~ 20:12:错误:请求从“堆栈*”转换为非标量类型“堆栈” 堆栈 s = 新堆栈(); ^~~~~~~~~~~

【问题讨论】:

  • if(empty()) return true 在函数 int pop()int top() 中。没有经历所有事情,但这两个绝对是不正确的
  • @mangusta 请解释一下。
  • @mangusta 嗯?

标签: c++ c++11 visual-c++ c++14 c++17


【解决方案1】:

错误来自 ma​​in.cpp 中的行:

Queue q = new Queue();

new 关键字创建一个指向类对象的指针,因此正确的语法是:

Queue *q = new Queue();

这也显示在此处的 C++ 教程文档中:http://www.cplusplus.com/doc/tutorial/classes/#pointers_to_classes

Stack 指针变量也是如此。

请注意,这也意味着使用对象的语法也必须改变。

代替:

s.pop();

您需要将其修改为:

(*s).pop();

s->pop();

希望这会有所帮助!

【讨论】:

  • 另外,参考@mangusta 提到的内容。 (由于新声誉无法评论):类函数pop()top()的定义指定了int的返回类型。但是,正如@mangusta 所提到的,如果empty() 返回true,这将违反指定的返回类型,因为它将尝试返回bool 而不是int
  • 这是一个 logic 错误,而不是 code 错误。 C++ 语言具有从boolint 的明确定义的隐式转换,反之亦然。如果堆栈为空,则没有可返回的内容,因此代码应该抛出异常,而不是通过返回一个在堆栈不为空时可能存储在堆栈中的值来混淆调用者。
  • @RemyLebeau 这是一个很好的观点,感谢您的澄清。
  • 此外,错误与转换有关的原因是单参数构造函数(“Queue(int cap)”)试图进行这样的转换。使用显式关键字声明单参数构造函数通常是一个好主意,除非您打算将它们用于转换。
  • @EvanGiarta 现在程序崩溃了。 :(
【解决方案2】:

当您使用new 关键字进行实例化时,您会创建一个指向该对象的指针。因此,正确的实例化如下:

Queue * q = new Queue();

Stack * s = new Stack();

x是指针时,x的值是对象的地址,*x是实际的对象。

【讨论】:

  • 现在程序崩溃了。 :(
【解决方案3】:

我不同意建议让它与new 一起工作的答案。这是不必要的。

不要使用new,只需将它们保留为普通堆栈变量即可。这样您就不必为以后的delete 感到负担,也不必将. 的所有实例替换为-&gt;

简单

Queue q;
Stack s;

您的程序在其他方面保持不变。

(您可能来自 背景吗?new 在那里是必需的,但在 C++ 中通常不需要)

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2015-01-25
    • 1970-01-01
    • 2014-12-25
    • 2010-09-10
    • 2020-07-11
    相关资源
    最近更新 更多