2015-06-30 140 views
1

首先,我知道这不是最好的方式来做到这一点,我只是看它应该如何完成。我创建了一个名为BORD类,其中包含一个成员C++使用unique_ptr的字符数组

 std::unique_ptr<std::unique_ptr<char>[] > char_bord; 

这应该是正确的语法,然后我尝试在构造函数初始化此:

bord::bord():char_bord(new std::unique_ptr<char>[10]) 
{ 
    //char_bord=new std::unique_ptr<char>[10]; //This did not seem to work aswell. 
    for(int i=0;i<10;i++) 
     char_bord[i]=new std::unique_ptr<char>[](new char[10]); 
    return; 
} 

这将导致错误的下面堆,我没有设法破译。

[email protected]:~/Git/Board/lib$ g++ -std=c++0x bord.c 
In file included from bord.c:1:0: 
bord.h:20:1: error: new types may not be defined in a return type 
class bord 
^ 
bord.h:20:1: note: (perhaps a semicolon is missing after the definition of ‘bord’) 
bord.c:3:12: error: return type specification for constructor invalid 
bord::bord():char_bord(new std::unique_ptr<char>[10]) 
      ^
bord.c: In constructor ‘bord::bord()’: 
bord.c:7:46: error: expected primary-expression before ‘]’ token 
     char_bord[i]=new std::unique_ptr<char>[](new char[10]); 
              ^
bord.c:7:60: error: parenthesized initializer in array new [-fpermissive] 
     char_bord[i]=new std::unique_ptr<char>[](new char[10]); 
                  ^
bord.c:7:19: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<char>’ and ‘std::unique_ptr<char>*’) 
     char_bord[i]=new std::unique_ptr<char>[](new char[10]); 
       ^
bord.c:7:19: note: candidates are: 
In file included from /usr/include/c++/4.9/memory:81:0, 
       from bord.h:19, 
       from bord.c:1: 
/usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = char; _Dp = std::default_delete<char>] 
     operator=(unique_ptr&& __u) noexcept 
    ^
/usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::unique_ptr<char>&&’ 
/usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = char; _Dp = std::default_delete<char>] 
    operator=(unique_ptr<_Up, _Ep>&& __u) noexcept 
^
/usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template argument deduction/substitution failed: 
bord.c:7:19: note: mismatched types ‘std::unique_ptr<_Tp, _Dp>’ and ‘std::unique_ptr<char>*’ 
     char_bord[i]=new std::unique_ptr<char>[](new char[10]); 
       ^
In file included from /usr/include/c++/4.9/memory:81:0, 
       from bord.h:19, 
       from bord.c:1: 
/usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = char; _Dp = std::default_delete<char>; std::nullptr_t = std::nullptr_t] 
     operator=(nullptr_t) noexcept 
    ^
/usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::nullptr_t’ 

我做错了什么,假设我做错了什么。

+1

为什么不简单地使用'std :: vector '? –

+0

我知道这是要走的路,我只是想知道为什么这不起作用。 – jelmew

+0

什么'bord :: bord():char_bord'应该声明?它是'bord :: bord :: char_bord'(如果你还有一个'bord'命名空间)或者简单地'bord :: char_bord'(如果你的'bord'类在全局命名空间中)?哦,是的... btw ...为什么要用数组而不是使用C++容器? –

回答

3

下面是一些代码,演示了什么,我想你想:

#include <iostream> 
#include <algorithm> 
#include <iterator> 
#include <memory> 

using namespace std; 

using array_ptr_type = std::unique_ptr<char[]>; 
using array_of_arrays_type = std::unique_ptr<array_ptr_type[]>; 

auto main() -> int 
{ 
    auto x = array_ptr_type(new char[10]); 
    auto y = array_ptr_type(new char[10]); 
    for (int i = 0 ; i < 10 ; ++i) 
    { 
     x[i] = char('0' + i); 
     y[i] = char('0' + 9 - i); 
    } 

    auto pxy = array_of_arrays_type(new array_ptr_type[2]); 
    pxy[0] = move(x); 
    pxy[1] = move(y); 

    for (int i = 0 ; i < 2 ; ++i) { 
     copy(&pxy[i][0], &pxy[i][10], ostream_iterator<char>(cout, ", ")); 
     cout << endl; 
    } 
    return 0; 
} 

预期输出:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 

当然,如你所知,这一切都不建议 - vector<vector<char>>会很多更清洁,更易于维护。

+3

嗨,谢谢,那正是我想知道的,它将如何完成。事实上,回到更合理的矢量>。谢谢你回答我的好奇心。 – jelmew

2

你必须使用专门:

std::unique_ptr<char[]> chars(new char[1024]); 

这是因为std::unique_ptr不支持定制删除作为std::shared_ptr确实(在此写作风格)。

std::unique_ptr使用std::default_delete作为删除者。不久,如果您指定参数类型为class T它将使用默认delete,但如果您编写class T[](在此专业化)std::unique_ptr将使用delete[]

但是最好使用一些容器而不是c风格的数组。

+0

我kkow我应该使用一个容器。你能否详细说明你的答案?它对我来说并不完全清楚。这只是一个智能指针,现在指向一个愚蠢的指针列表? – jelmew

+0

什么是'std :: unique_ptr'和其他智能指针的用法?只是在超出范围时正确删除指针,或者它的引用计数为零。你的'std :: unique_ptr []>是为了什么?当你想要删除'unique_ptr'时......我甚至无法描述那些无意义的代码。如果你想制作一个智能指针到字符数组 - 使用一个智能指针作为数组。否则我不明白你想做什么。 –

+0

这......在技术上是正确的,但并不能解释OP在他的问题中遇到的所有编译器错误。 – Puppy