英文:
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 <vector>
#include "Cube.h"
#include <ostream>
namespace uiuc{
class Stack{
private:
std::vector<Cube> stackOfCubes;
int size_;
public:
Cube& 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& operator<<(std::ostream& os, const Stack& 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<<
, 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<uiuc::Cube, std::allocator<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&
reference with a const std::vector<Cube>
object, which is what your return stack.stackOfCubes;
statement is trying to do.
Your operator<<
should not return
the stack at all, it needs to return
the provided ostream
instead. Your operator<<
should write the contents of the stack to that stream, eg:
friend std::ostream& operator<<(std::ostream& os, const Stack& stack){
// iterate stack.stackOfCubes, writing each Cube to os as needed...
return os;
};
For example:
class Cube : public Shape {
...
friend std::ostream& operator<<(std::ostream& os, const Cube& cube){
// write cube members to os as needed...
return os;
}
};
class Stack {
...
friend std::ostream& operator<<(std::ostream& os, const Stack& stack){
for(const Cube &cube : stack.stackOfCubes) {
os << 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<<
that accepts a Cube
and writes it to the output stream. The signature would be something like,
std::ostream& operator<<(std::stream& os, const Cube& cube);
And, then, you can implement your stack output with a simple loop,
friend std::ostream& operator<<(std::ostream& os, const Stack& stack) {
os << "{ ";
for (const auto& elem : stack.stackOfCubes)
os << elem << " ";
os << "}";
return os;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论