难道是返回值的问题?

bingshanzhiling 2008-11-07 09:53:05
前几天在写一个程序的时候感觉到有必要把自己的栈给扩充一下,就是添加一些方法,可以在以后比较方便的写代码,但是在扩充这个reverse_stack的时候不管怎样就是不行,问题的关键个人觉得是由于node类,linkstack类涉及到了new和delete,返回的时候已经delete了... ...最初的时候是想返回值,但是不行,返回引用又是局部变量,于是想返回一个static,依然不行,返回一个static的引用还是不行... ...没有想到的是,思路如此简单却难以实现... ...
操作系统vista,编译器vs2008.
特发此贴一问究竟....代码及测试代码见附件... ...
...全文
129 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzh9955 2008-11-09
  • 打赏
  • 举报
回复
期待中!!!
jieao111 2008-11-08
  • 打赏
  • 举报
回复
还没结贴,再扯点别的,相信你在调试的过程中一定是焦头烂额。。那么究竟是什么原因造成这种情况的呢,你一直认为是返回值的问题,可是试了很多种方法后,发现问题越来越多。。。


其实调试也是一门学问,网上也有相关文章,感兴趣的话可以搜搜。。我记得《程序员》上曾经有篇文章叫“调试之剑”什么的反响好像不错。。

记得我以前看过的书上说,第一步,首先多次测试,尽量使错误“同一化”,意思就是要错误出现的情况相似。。这样就可以进一步猜测错误的原因,象你的做法就是猜测是返回值的原因,可是有一点要注意的就是,不要在“一头走到死”。。意思就是不要抓住一个想法不变,要及时换个思路。。比如你试了多种方法都没对,这时就应该猜测一下别的原因了,,比如:
[Quote=引用 20 楼 jieao111 的回复:]
你要清楚,返回一个对象一定的是正确的,出错了,很可能就是实现的原因
[/Quote]

太乙 2008-11-08
  • 打赏
  • 举报
回复


大概看了一下,lz的问题嘛:

就在这个函数上:
linkstack<T>& reverse_stack()const;//返回当前栈的逆序栈;

修改方法:

第一:
linkstack(const linkstack<T> &)
{
//实现你的复制构造函数
}

然后将函数改为:
linkstack<T> reverse_stack()const;//返回当前栈的逆序栈;

{
linkstack<T> _reverse;//这句话乃至这个函数应该怎样写?
node<T> *_temp;
_temp=top;
while(_temp)
{
_reverse.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
return _reverse;

}

第二:

我帮你改好了:





#include<iostream>
using namespace std;


#ifndef _linkstack_
#define _linkstack_
template<class T>
class linkstack;
template<class T>
class node
{
friend linkstack<T>;
private:
T data;
node *link;
};
template<class T>
class linkstack
{
private:
node<T> *top;
public:
linkstack(){top=0;}

~linkstack();
bool isempty()const{ return top==0;}
T pop() const;
void output() const;
linkstack<T>& push(const T & x);
linkstack<T>& s_delete(T& x);
void r_output() const; //实现倒叙输出;
linkstack<T>& operator=(const linkstack<T>& rightop);
linkstack<T>* reverse_stack()const;//返回当前栈的逆序栈;
};
template<class T>
linkstack<T>::~linkstack()
{
node<T> *next;
while(top)
{
next=top->link;
delete top;
top=next;
}
} // /*////
template<class T>
T linkstack<T>::pop()const
{
if(!isempty())
return top->data;
}
template<class T>
linkstack<T>& linkstack<T>::push(const T&x)
{
node<T> *pointer=new node<T>;
pointer->data=x;
pointer->link=top;
top=pointer;
return *this;
}
template<class T>
linkstack<T>& linkstack<T>::s_delete(T &x)
{
if(!isempty())
{
x=top->data;
node<T> *pointer=top;
top=top->link;
delete pointer;
return *this;
}
}
template<class T>
void linkstack<T>::output() const
{
node<T> *temp;
temp=top;
while(temp)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
template<class T>
void linkstack<T>::r_output() const
{
linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_index; //_index是倒序的索引;
_temp=this->top;
while(_temp)
{
_reverse_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈;
_index=_reverse_stack.top;
while(_index)
{
cout<<_index->data<<" ";
_index=_index->link;
}
cout<<endl;
}
template<class T>
linkstack<T>& linkstack<T>::operator=(const linkstack<T>& rightop)
{//返回一个与rightop内容相同的栈;
if(this==&rightop)
return *this;
linkstack<T> temp_stack;
// linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_this; //用来清空*this;
node<T> *_index; //_index是倒序的索引;
_temp=rightop.top;
while(_temp)
{
temp_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
while(top)
{//清空*this的数据;
_this=top->link;
delete top;
top=_this;
}
_index=temp_stack.top;
while(_index)
{
this->push(_index->data);
_index=_index->link;
}
return *this;
}
template<class T>
linkstack<T>* linkstack<T>::reverse_stack()const
{
linkstack<T> *_reverse = new linkstack<T>();//这句话乃至这个函数应该怎样写?
node<T> *_temp ;//, *p_temp;
_temp=top;
while(_temp)
{
_reverse->push(_temp->data);
// p_temp = _temp;
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
// _reverse->top = p_temp;
return _reverse;
} ////*/
#endif

int main()
{
linkstack<int> src;
for(int i=0;i<10;i++)
src.push(i);
src.output();
linkstack<int>* des=src.reverse_stack();
des->output();
delete des;
return 0;
}



jieao111 2008-11-08
  • 打赏
  • 举报
回复
你要清楚,返回一个对象一定的是正确的,出错了,很可能就是实现的原因
bingshanzhiling 2008-11-08
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 hqin6 的回复:]
C/C++ code

大概看了一下,lz的问题嘛:

就在这个函数上:
linkstack<T>& reverse_stack()const;//返回当前栈的逆序栈;

修改方法:

第一:
linkstack(const linkstack<T> &)
{
//实现你的复制构造函数
}

然后将函数改为:
linkstack<T> reverse_stack()const;//返回当前栈的逆序栈;

{
linkstack<T> _reverse;//这句话乃至这个函数应该怎样写?
node<T> *_temp;
_temp=top;

[/Quote
一个问题,这是effective c++上面讨论的,不能返回一个局部new,因为没人记得在函数外delete... ...
chaojiew 2008-11-08
  • 打赏
  • 举报
回复
mark
jieao111 2008-11-07
  • 打赏
  • 举报
回复
d
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jieao111 的回复:]
return的时候会产生一个临时对象,这时调用的是拷贝构造函数。。
[/Quote]
谢谢,应该会是这样,谢谢,解决了关键问题...
jieao111 2008-11-07
  • 打赏
  • 举报
回复
return的时候会产生一个临时对象,这时调用的是拷贝构造函数。。
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jia_xiaoxin 的回复:]
最主要的原因是你在类中没有创建拷贝构造函数:


[/Quote]
有道理啊,不过linkstack<T>这个返回值貌似是直接用的那个operator=赋值给des的啊... ...
frank_ll 2008-11-07
  • 打赏
  • 举报
回复
mark
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
我是楼主,个人感觉问题的关键在于局部变量析构函数会删除栈里面的内容,依据是
template<class T>
linkstack<T>::~linkstack()
{
node<T> *next;
while(top)
{
next=top->link;
delete top;
top=next;
}
}
delete应该是罪魁祸首... ...
jia_xiaoxin 2008-11-07
  • 打赏
  • 举报
回复
最主要的原因是你在类中没有创建拷贝构造函数:

template<class T>
class linkstack
{
private:
node<T> *top;
public:
linkstack(){top=0;}
linkstack(const linkstack<T>& rightop);
~linkstack();
bool isempty()const{ return top==0;}
T pop() const;
void output() const;
linkstack<T>& push(const T & x);
linkstack<T>& s_delete(T& x);
void r_output() const; //实现倒叙输出;
linkstack<T>& operator=(const linkstack<T>& rightop);
linkstack<T> reverse_stack()const;//返回当前栈的逆序栈;
};

template<class T>
linkstack<T>::linkstack(const linkstack<T>& rightop)
{
T x;
this->s_delete(x);

linkstack<T> temp_stack;
// linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_this; //用来清空*this;
node<T> *_index; //_index是倒序的索引;
_temp=rightop.top;
while(_temp)
{
temp_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
while(top)
{//清空*this的数据;
_this=top->link;
delete top;
top=_this;
}
_index=temp_stack.top;
while(_index)
{
this->push(_index->data);
_index=_index->link;
}

...


template<class T>
linkstack<T> linkstack<T>::reverse_stack()const
{
linkstack<T> _reverse;//这句话乃至这个函数应该怎样写?
node<T> *_temp;
_temp=top;
while(_temp)
{
_reverse.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
return _reverse;
} ////*/


}
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqin6 的回复:]
解决了?
jf了哈~~~
[/Quote]
没有啊...解决了贴代码,谢谢,给分...
太乙 2008-11-07
  • 打赏
  • 举报
回复
解决了?
jf了哈~~~
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuedaoli 的回复:]
定义了个局部变量

linkstack <T> _reverse,
把当前栈得值临时存到_reverse中,清空当前得栈。
再把_reverse重新赋到当前得栈中,返回*this

template <class T>
linkstack <T>& linkstack <T>::reverse_stack()const
{
linkstack <T> _reverse;//这句话乃至这个函数应该怎样写?
node <T> *_temp;
_temp=top;
while(_temp)
{
_reverse.push(_temp->data);
_tem…
[/Quote]
主函数怎么调用?
des=src.reverse_stack()?
当前的this指针应该是src吧?
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jieao111 的回复:]
引用楼主 bingshanzhiling 的帖子:
返回引用又是局部变量
此话怎讲
[/Quote]
不能返回对局部变量的引用!
wangguilin 2008-11-07
  • 打赏
  • 举报
回复
up
study
bingshanzhiling 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jieao111 的回复:]
返回一个对象就行了linkstack <T> linkstack <T>::reverse_stack()const
[/Quote]
那是不行的,不信你可以试试... ...
jieao111 2008-11-07
  • 打赏
  • 举报
回复
返回一个对象就行了linkstack <T> linkstack <T>::reverse_stack()const
加载更多回复(5)

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧