【发布时间】:2015-06-30 18:38:27
【问题描述】:
首先,我知道这不是最好的方法,我只是在研究应该如何做。我创建了一个名为bord的类,它拥有一个成员
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;
}
这会导致以下一堆错误,我没有设法破译。
jelmer@jelmer-N56JN:~/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’
假设我做错了什么,我做错了什么。
【问题讨论】:
-
你为什么不简单地使用
std::vector<char>? -
我知道这是要走的路,我只是想知道为什么这不起作用。
-
bord::bord():char_bord应该声明什么?是bord::bord::char_bord(如果您也有bord命名空间)还是只是bord::char_bord(如果您的bord类在全局命名空间中)?哦,是的……顺便说一句……为什么要玩数组而不是使用 C++ 容器? -
您的标头中有编译器错误,但没有显示标头。
标签: c++ c++11 unique-ptr