如何使用 std::vector 和 std::cout 在 C++ 中打印出我的 Stack 类的元素?

huangapple go评论60阅读模式
英文:

How can I print out the elements of my Stack class in C++ using std::vector and std::cout?

问题

我需要打印出我的 Stack 类的元素,但我不知道该如何做。

我目前正在解决汉诺塔问题,并为此创建了一个 Stack 类,它基本上是一个立方体的向量(我还创建了一个 Cube 类,它继承了一个 Shape 类):

Stack.h

#pragma once
#include <vector>
#include "Cube.h"
#include <ostream>

namespace uiuc {
    class Stack {
    private:
        std::vector<Cube> stackOfCubes;
        int size_;
    public:
        Cube& pop(); // 获取堆栈的最后一个立方体并移除它
        void push(Cube cube); // 将一个立方体推入堆栈
        void peek(); // 打印出整个堆栈
        int size();
        friend std::ostream& operator<<(std::ostream& os, const Stack& stack) {
            return stack.stackOfCubes;
        };
    };
}

我想要处理 peek() 方法,该方法打印出所有的立方体堆栈,但是我不知道如何做,因为 std::cout 不知道如何打印类对象。

我在其他问题中发现,我应该重载 operator<<,但是当我尝试返回 stackOfCubes 时出现以下错误:

不能用类型为 "std::ostream &&"(不带const限定符)的引用初始化 "const std::vector<uiuc::Cube, std::allocatoruiuc::Cube>" 的值。

英文:

I need to print out elements of my Stack class, but I have no idea how to do so.

I'm currently working on the Tower of Hanoi problem, and to do so I have created a Stack class, which is basically a vector of cubes (created also the Cube class, which is inheriting a Shape class):

Stack.h

#pragma once
#include &lt;vector&gt;
#include &quot;Cube.h&quot;
#include &lt;ostream&gt;

namespace uiuc{
    class Stack{
        private:
            std::vector&lt;Cube&gt; stackOfCubes;
            int size_;
        public:
            Cube&amp; pop();//gets the last cube of the stack and removes it
            void push(Cube cube);//pushes a cube into the stack 
            void peek();//prints out ALL the stack
            int size();
        friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Stack&amp; stack){
            return stack.stackOfCubes;
        };
    };
}

I want to work on the peek() method, which prints out ALL the stack of cubes, but nonetheless I have no idea how to do so since std::cout doesn't know how to print out class objects.

I have found on other questions that I should overload operator&lt;&lt;, but it says the following error when I try to return the stackOfCubes:

> a reference of type "std::ostream &" (not const-qualified) cannot be initialized with a value of type "const std::vector&lt;uiuc::Cube, std::allocator&lt;uiuc::Cube>>

答案1

得分: 1

错误消息告诉您,您不能使用const std::vector<Cube>对象初始化std::ostream&引用,而您的return stack.stackOfCubes;语句正试图这样做。

您的operator<<根本不应该返回堆栈,而是需要返回提供的ostream。您的operator<<应该将堆栈的内容写入该流,例如:

friend std::ostream& operator<<(std::ostream& os, const Stack& stack){
    // 迭代stack.stackOfCubes,根据需要将每个Cube写入os...
    return os;
};

例如:

class Cube : public Shape {
    ...
    friend std::ostream& operator<<(std::ostream& os, const Cube& cube){
        // 根据需要将cube成员写入os...
        return os;
    }
};

class Stack {
    ...
    friend std::ostream& operator<<(std::ostream& os, const Stack& stack){
        for(const Cube &cube : stack.stackOfCubes) {
            os << cube;
        }
        return os;
    }
};
英文:

The error message is telling you that you can't initialize a std::ostream&amp; reference with a const std::vector&lt;Cube&gt; object, which is what your return stack.stackOfCubes; statement is trying to do.

Your operator&lt;&lt; should not return the stack at all, it needs to return the provided ostream instead. Your operator&lt;&lt; should write the contents of the stack to that stream, eg:

friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Stack&amp; stack){
    // iterate stack.stackOfCubes, writing each Cube to os as needed...
    return os;
};

For example:

class Cube : public Shape {
    ...
    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Cube&amp; cube){
        // write cube members to os as needed...
        return os;
    }
};

class Stack {
    ...
    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Stack&amp; stack){
        for(const Cube &amp;cube : stack.stackOfCubes) {
            os &lt;&lt; cube;
        }
        return os;
    }
};

答案2

得分: 0

你需要提供一个重载 operator<< 的函数,它接受一个 Cube 并将其写入输出流。函数签名可能如下所示,

std::ostream& operator<<(std::ostream& os, const Cube& cube);

然后,你可以使用一个简单的循环来实现堆栈的输出,

friend std::ostream& operator<<(std::ostream& os, const Stack& stack) {
    os << "{ ";
    for (const auto& elem : stack.stackOfCubes)
        os << elem << " ";
    os << "}";
    return os;
}
英文:

You need to provide an overload for operator&lt;&lt; that accepts a Cube and writes it to the output stream. The signature would be something like,

std::ostream&amp; operator&lt;&lt;(std::stream&amp; os, const Cube&amp; cube);

And, then, you can implement your stack output with a simple loop,

friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Stack&amp; stack) {
    os &lt;&lt; &quot;{ &quot;;
    for (const auto&amp; elem : stack.stackOfCubes)
        os &lt;&lt; elem &lt;&lt; &quot; &quot;;
    os &lt;&lt; &quot;}&quot;;
    return os;
}

huangapple
  • 本文由 发表于 2023年5月28日 02:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定