C++:复制指向对象的指针堆栈?
我正在尝试为一个赋值制作一个程序,它使用基于链表的堆栈,其中包含指向包含字符串对象的节点的指针。 我想打印stack rod,它利用pop,每次迭代都会删除head对象,直到它为空。所以我需要找到一种方法在打印之前备份rod[]。 我尝试将rod[]的内容推入rodCopy[],然后调用rod[x]=rodCopy[x],这应该会将rod[]恢复到其原始状态,但这似乎不起作用,因为我再次开始出现seg故障。 另一个问题是它向后复制堆栈,因为它存储最近存储的对象。 例如:C++:复制指向对象的指针堆栈?,c++,string,templates,pointers,segmentation-fault,C++,String,Templates,Pointers,Segmentation Fault,我正在尝试为一个赋值制作一个程序,它使用基于链表的堆栈,其中包含指向包含字符串对象的节点的指针。 我想打印stack rod,它利用pop,每次迭代都会删除head对象,直到它为空。所以我需要找到一种方法在打印之前备份rod[]。 我尝试将rod[]的内容推入rodCopy[],然后调用rod[x]=rodCopy[x],这应该会将rod[]恢复到其原始状态,但这似乎不起作用,因为我再次开始出现seg故障。 另一个问题是它向后复制堆栈,因为它存储最近存储的对象。 例如: output: rod[
output:
rod[0] = "x", "xx", "xxx" //"x" is the top() object but "xxx" was the first
rodCopy[0] = "xxx", "xx", "x" // "x" is the first object, "xxx" is the top()
TowerHanoi.h文件:
LStack.template文件:
Node.template set_数据和set_链接:
LStack是否有复制构造函数?看起来您需要为LStack和LinkedList定义有意义的复制构造函数。我没有复制构造函数。这件事怎么办?在调用LStack时,它调用list=new LinkedList;在调用node*obj=new node时,它初始化要创建的下一个节点NULL@CameronOReilly copy构造函数应该在你的入门书中解释。我认为给定的addToHead会在每次我将新对象推入head节点时分配内存,这将创建一个副本。
#ifndef TOWERHANOI_CAMERON_H
#define TOWERHANOI_CAMERON_H
#include "LStack.h"
namespace oreilly_A2 {
class TowerHanoi {
public:
TowerHanoi();
TowerHanoi(size_t numDiscs);
LStack<std::string> get_Discs();
void set_Discs(size_t disc);
void print_Game();
LStack<std::string> copyRod(int r);
void setRod(LStack<std::string> r1, LStack<std::string> r2);
private:
LStack<std::string> rod[3];
int discs, source, target;
};
}
#endif
void TowerHanoi::print_Game() {
string xString;
LStack<string> rodCopy = rod[0];
//string spacing;
//int spaceInt= discs-1;
for (size_t a=0; a<discs;a++) {
xString = rod[0].pop();
cout << xString; //print rod[0]
if (!rod[1].empty()) { //is rod 2 empty?
cout << " ";
xString = rod[1].pop();
cout << xString; //print rod[1]
}
cout << endl;
}
string bottomRow; //print the bottom row after printing rods
for (int c=0; c < (((discs*2)-1)+3);c++) {
bottomRow.append("-");
}
string rodNum;
for (int c=0; c < (discs/2);c++) {
rodNum.append("-");
}
rodNum.append("rod 1");
cout << bottomRow << endl;
cout << rodNum << endl;
}
namespace oreilly_A2 {
template <typename Obj>
LStack<Obj>::LStack() {
list = new LinkedList<Obj>();
used=0;
}
template <typename Obj>
LStack<Obj>::~LStack() {
delete list;
}
//push
template <typename Obj>
void LStack<Obj>::push(Obj head_in) {
list->addToHead(head_in);
used++;
}
//pop
template <typename Obj>
Obj LStack<Obj>::pop() {
used--;
return list->removeFromHead();
}
//top
template <typename Obj>
const Obj& LStack<Obj>::top() {
return list->list_getHead();
}
//empty
template <typename Obj>
bool LStack<Obj>::empty() {
if (list->getEmpty()) {
return true;
}
return false;
}
//size
template <typename Obj>
int LStack<Obj>::size() const {
return used;
}
}
template <typename Item>
void LinkedList<Item>::addToHead(Item entry) {
if (head == NULL) {
head = new node<Item>(entry, NULL);
}
else {
head = new node<Item>(entry, head);
}
}
template <typename Item>
const Item LinkedList<Item>::removeFromHead() {
node<Item>* tmp_ptr;
tmp_ptr = head->link();
std::string head_copy;
head_copy = head->data(); //create a copy of head
head = tmp_ptr;
//delete tmp_ptr;
//head->set_data(head->link()); //set head's data to the previous object
return head_copy; //return head's original data
//delete head_copy;
}
template <typename Object>
void node<Object>::set_data(const Object& new_data){
std::string new_string;
new_string = new_data;
word = new_string;
}
template <typename Object>
void node<Object>::set_link(node<Object>* new_link){
next= new_link;
}