c++ - 为什么我会收到 "invalid conversion from ' Queue*/Stack *' to ' int'"错误消息?

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

为什么会出现这些错误?

invalid conversion from 'Queue*' to 'int'

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

我试过修改 QueueStack,但无济于事。我正在做一个作业,它使用 Queue 实现 Stack 并使用 Stack 实现 Queue

堆栈.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();
            ^~~~~~~~~~~

最佳答案

错误将来自 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();

希望这对您有所帮助!

关于c++ - 为什么我会收到 "invalid conversion from ' Queue*/Stack *' to ' int'"错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58278291/

相关文章:

c++ - std::map 中键/值类型的复制/移动要求?

c++ - "virtual void IBase::Foo"和 "virtual void Foo"之间有什么区别吗?

c++ - (C++) 需要帮助解决输出文件错误

c++ - boost中的单例如何实现在调用main之前初始化所有单例?

c# - 在 C# 和 C++ 之间共享变量

c++ - 合并来自 std::set 的相邻条目

c# - 将 C++(处理 Corona)函数转换为 C#

c++ - std::bind 和 std::function 问题

visual-c++ - 在MSVC中,为什么InterlockedOr和InterlockedAnd生成循环而不是简单的锁定指令?

c++ - 消息映射宏