linux怎么编译sharedptr,如何使用智能指针(例如auto_ptr r shared_ptr)在Linux上使用C++生成链接列表数据结构?...

我需要生成一个列表并返回一个指针,以便其他函数可以使用该列表。该代码工作,但有内存泄漏,因为我使用“新”分配列表的每个新节点。

使用列表后,我必须释放内存。

我的代码如下:

#include

#include

#include

using namespace std;

class linkListClass

{

private:

int data;

auto_ptr nextData;

public:

auto_ptr buildLinkList(const int size);

int getData(){return data;};

int printListBackward(auto_ptr listroot);

};

inline auto_ptr linkListClass::buildLinkList(const int size)

{

linkListClass *trueRoot;

linkListClass *listRoot = new linkListClass ;

linkListClass *trueRoot;

linkListClass *listRoot = new linkListClass ;

// data is random int

for (int i = 0; i < size ; ++i)

{

if (i < size -1)

{

if (i == 0)

{

listRoot->data = rand()%10 ;

listRoot->nextData = auto_ptr(0) ;

trueRoot = listRoot ; // transfer ownership

}

else{

listRoot->nextData = auto_ptr (new linkListClass); // segmentation fault

listRoot->data = rand()%10 ;

listRoot = listRoot->nextData ;

}

}

else

listRoot->nextData = auto_ptr(0) ;

}

cout << "the built list has " << size << " data \n\n" ;

return trueRoot;

}

inline int linkListClass::printListBackward(auto_ptr listroot)

{

int counter =0 ;

stack outputStack;

cout << "print the list forward \n\n" ;

//if (listroot != NULL)

cout << "print the list forward \n\n" ;

//if (listroot != NULL)

if (listroot.get() != 0)

{

do

{

try{

cout << listroot->getData() << " \t " ;

outputStack.push(listroot->data);

listroot = listroot->nextData;

++counter;

cout << "in printListBackward counter is " << counter << endl;

//if (listroot == 0) break;

}

catch(exception& e)

{

cout << "an error is " << e.what() << endl;

return 1;

}

//}while(listroot != 0);

}while(listroot.get() != 0);

cout << "in printListBackward outof do while \n\n " << endl ;

}

else

{

cout << "the input list is null \n\n" << endl;

return 1;

}

cout << endl ;

cout << "there are" << counter << " data in the list \n\n " << endl ;

cout << "print the list backward \n\n" ;

if (outputStack.empty() == 1)

{

cout << "the ouytput queu is empty \n\n " << endl ;

cout << "the ouytput queu is empty \n\n " << endl ;

return 1;

}

else

{

do

{

cout << outputStack.top() << " \t" ;

outputStack.pop();

}while(outputStack.empty() == 0);

}

cout << endl;

cout << "there are" << counter << " data in the list \n\n " << endl ;

return 0 ;

}

int main()

{

const int listSize = 5;

linkListClass linkListObj;

auto_ptr myRoot ; //= linkListClass::buildLinkList(listSize);

myRoot = linkListObj.buildLinkList(listSize);

linkListObj.printListBackward(myRoot);

return 0;

}

// EOF

的代码有段错误,因为auto_ptr的传递指针对象的所有权,使 后

listRoot = listRoot->nextData

链表被打破,listRoot-> nextData一片空白。

我已经试过TR1 :: shared_ptr的和waek_ptr

tr1::weak_ptr wp1 = listRoot->nextData;

listRoot = wp1.lock() ;

listRoot = listRoot->nextData ;

但我得到了编译错误:

listPtSharedptr.cpp:63: error: conversion from linkListClass* to non-scalar type std::tr1::weak_ptr requested listPtSharedptr.cpp:65: error: no match for operator= in listRoot = listRoot.std::tr1::shared_ptr<_tp>::operator-> with _Tp = linkListClass->linkListClass::nextData /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:486: note: candidates are: std::tr1::shared_ptr& std::tr1::shared_ptr::operator=(const std::tr1::shared_ptr&)

任何帮助将不胜感激。

的感谢!

+0

我不认为你想传递一个auto_ptr <>到printLastBackward(),而只是传递一个常规指针。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值