- 论坛徽章:
- 0
|
刚刚在测试list,auto_ptr的相关使用方法,但是代码编译通不过!请大家帮忙解决。
代码如下
- #include <list>;
- #include <vector>;
- #include <memory>;
- #include <iostream>;
- using namespace std;
- class test
- {
- public:
- test( int k = 0 )
- {
- i = k;
- cout << "test BBBB - " << i << endl;
- }
- ~test()
- {
- cout << "test DDDD - " << i << endl;
- }
- test( const test& t )
- {
- this->;i = t.i;
- }
- const test& operator=( const test& t )
- {
- this->;i = t.i;
- return *this;
- }
- private:
- int i;
- };
- typedef auto_ptr<test>; test_ptr;
- typedef list<test_ptr>; test_list;
- int main()
- {
- test_list tl;
- for( int i = 0; i < 10; i++ )
- {
- test_ptr ptr( new test( i ) );
- tl.push_back( ptr );
- }
- }
复制代码
编译提示
/usr/include/g++-3/stl_construct.h: In function `void construct (_T1 *,
const _T2 & [with _T1 = test_ptr, _T2 = auto_ptr<test>;]':
/usr/include/g++-3/stl_list.h:293: instantiated from `list<_Tp, _Alloc>;::_M_create_node (const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
/usr/include/g++-3/stl_list.h:344: instantiated from `list<_Tp, _Alloc>;::insert (_List_iterator<_Tp, _Tp &, _Tp *>;, const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
/usr/include/g++-3/stl_list.h:381: instantiated from `list<_Tp, _Alloc>;::push_back (const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
a.cpp:44: instantiated from here
/usr/include/g++-3/stl_construct.h:48: passing `const auto_ptr<test>;'
as `this' argument of `auto_ptr<_Tp>;:perator
auto_ptr<_Tp>;::auto_ptr_ref<_Tp1>; () [with _Tp1 = test, _Tp = test]'
discards qualifiers |
|