[求助]把字符串压入栈中
把字符串压入栈中,主要问题在红字处,谢谢大家帮我看看于是写了如下程序,
#include <iostream>
#include <cstring>
using namespace std;
class Stack //l类的声明
{
struct Link
{
void * data;
Link * next;
Link(void * dat,Link * nxt);
~Link();
}* head;
public:
Stack();
~Stack();
Stack(Stack * p[5]);//构造函数的参数是指向对象的指针数组,
void push(void * dat);
void * peek();
void * pop();
};
Stack::Link::Link(void * dat,Link * nxt)//类的定义
{
data = dat;
next = nxt;
}
Stack::Link::~Link()
{
}
Stack::Stack()
{
head = 0;
}
Stack::Stack(Stack * p[5])//构造函数的参数是指向对象的指针数组,便历该数组并把指针压入stack中
{
for(int i = 0;i < 5;i++)//便历该数组并把指针压入stack中
push((Stack *) p[i]);
};
void Stack::push(void * dat)
{
head = new Link(dat,head);
}
void * Stack::peek()
{
return head->data;
}
void * Stack::pop()
{
if(head == 0) return 0;
void * result = head->data;
Link * oldHead = head;
head = head->next;
delete oldHead;
return result;
}
Stack::~Stack()
{
}
#include "Stack3.h"
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc,char * argv[])
{
string pp[]={"Mondat","Tuesday","wednesday","Thursda","Friday"};//字符传数组
Stack sk1[5];
for(int i = 0;i < 5;i++)
{
sk1[i].push(&pp[i]);//压入字符传数组
cout<<*(string*)sk1[i].pop()<<" ";//想让他输出字符串却是输出地址,于是我把这句改写成cout<<*(string*)sk1[i].pop()<<" ";编译时却出错,我该怎么改呢,谢谢大家
}
system("pause");
return 0;
}