如何使用智能指针(例如,auto#ptr shared#ptr)在C+中生成链接列表数据结构+;在Linux上? 这是一个C++编程问题。p>
我需要生成一个列表并返回一个指针,以便其他函数可以使用该列表。代码可以工作,但内存泄漏,因为我使用“new”为列表分配每个新节点 使用列表后,我必须释放内存 我的代码如下:如何使用智能指针(例如,auto#ptr shared#ptr)在C+中生成链接列表数据结构+;在Linux上? 这是一个C++编程问题。p>,c++,linux,segmentation-fault,shared-ptr,auto-ptr,C++,Linux,Segmentation Fault,Shared Ptr,Auto Ptr,我需要生成一个列表并返回一个指针,以便其他函数可以使用该列表。代码可以工作,但内存泄漏,因为我使用“new”为列表分配每个新节点 使用列表后,我必须释放内存 我的代码如下: #include <iostream> #include <stack> #include <memory> using namespace std; class linkListClass { private: int data; auto_ptr<li
#include <iostream>
#include <stack>
#include <memory>
using namespace std;
class linkListClass
{
private:
int data;
auto_ptr<linkListClass> nextData;
public:
auto_ptr<linkListClass> buildLinkList(const int size);
int getData(){return data;};
int printListBackward(auto_ptr<linkListClass> listroot);
};
inline auto_ptr<linkListClass> 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<linkListClass>(0) ;
trueRoot = listRoot ; // transfer ownership
}
else{
listRoot->nextData = auto_ptr<linkListClass> (new linkListClass ); // segmentation fault
listRoot->data = rand()%10 ;
listRoot = listRoot->nextData ;
}
}
else
listRoot->nextData = auto_ptr<linkListClass>(0) ;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}
inline int linkListClass::printListBackward(auto_ptr<linkListClass> listroot)
{
int counter =0 ;
stack<int> 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<linkListClass> myRoot ; //= linkListClass::buildLinkList(listSize);
myRoot = linkListObj.buildLinkList(listSize);
linkListObj.printListBackward(myRoot);
return 0;
}
// EOF
链接列表已断开,listRoot->nextData为空
我试过tr1::shared_ptr和waek_ptr
tr1::weak_ptr<linkListClass> wp1 = listRoot->nextData;
listRoot = wp1.lock() ;
listRoot = listRoot->nextData ;
tr1::弱\u ptr wp1=listRoot->nextData;
listRoot=wp1.lock();
listRoot=listRoot->nextData;
但我得到了编译错误:
listPtSharedptr.cpp:63:错误:从linkListClass*转换为
请求的非标量类型std::tr1::弱\u ptr
listPtSharedptr.cpp:65:错误:listRoot中的运算符=不匹配=
std::tr1::shared\u ptr::operator->with\u 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:注:候选项为:std::tr1::shared_ptr&
std::tr1::shared_ptr::operator=(常量)
std::tr1::共享\u ptr&)
任何帮助都将不胜感激
谢谢 这里是对“buildLinkList”函数的修改,现在应该可以使用了。 但有一点不同。您正在创建FIFO列表。此版本创建后进先出列表。我想FIFO是很难创建的只是自动PTR。之后,您可以尝试使用这个工作示例来使用共享_ptr并将其转换为FIFO列表
inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
{
auto_ptr<linkListClass> trueRoot(0);
auto_ptr<linkListClass> listRoot(0);
// data is random int
for (int i = 0; i < size ; ++i)
{
listRoot = auto_ptr<linkListClass> (new linkListClass );
listRoot->data = random()%10 ;
listRoot->nextData = trueRoot;
trueRoot = listRoot;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}
inline auto_ptr linkListClass::buildLinkList(const int size)
{
auto_ptr trueRoot(0);
auto_ptr listRoot(0);
//数据是随机整数
对于(int i=0;idata=random()%10;
listRoot->nextData=TrueTroot;
trueRoot=listRoot;
}
难道我不认为您要将auto_ptr传递给printlastbackup(),只需传递一个常规指针即可。
inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
{
auto_ptr<linkListClass> trueRoot(0);
auto_ptr<linkListClass> listRoot(0);
// data is random int
for (int i = 0; i < size ; ++i)
{
listRoot = auto_ptr<linkListClass> (new linkListClass );
listRoot->data = random()%10 ;
listRoot->nextData = trueRoot;
trueRoot = listRoot;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}