Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::boost::reference_包装器的数组未编译_C++_Arrays_Boost - Fatal编程技术网

C++ boost::boost::reference_包装器的数组未编译

C++ boost::boost::reference_包装器的数组未编译,c++,arrays,boost,C++,Arrays,Boost,我有一个带有boost::array类型的类,它存储对std::vector的引用。 我想构造一个引用数组(我正在尝试DI,或者我认为是这样),但到目前为止还没有成功 class RefHolder { public: boost::array<boost::reference_wrapper<std::vector<int> >, 3> sumsArray; }; class OriginalHolder { public: Or

我有一个带有
boost::array
类型的类,它存储对
std::vector
的引用。 我想构造一个引用数组(我正在尝试DI,或者我认为是这样),但到目前为止还没有成功

class RefHolder
{

public:

    boost::array<boost::reference_wrapper<std::vector<int> >, 3> sumsArray;

};

class OriginalHolder
{

public:

    OriginalHolder(boost::array<boost::reference_wrapper<std::vector<int> >, 3> & pSumsArray)
    {
        sumsArray[0] = boost::ref(_sums);
    }

private:

    std::vector<int> _sums;

}

int main()
{
    RefHolder ref;

    OriginalHolder original(ref.sumsArray);

    return 0;
}
当然,我可以只使用普通指针,它是有效的,但我仍然有疑问,如果我能解决它,我宁愿使用引用

我正在使用QNX 6.4.1和GCC 4.3.3


提前感谢您的帮助=)

当您最初创建数组时,您有“空”条目。引用不能真的是空的(好的,它可能是空的,但这是一个非常糟糕的主意),所以引用不能作为数组条目工作

考虑改用
boost::smart\u ptr>
。我意识到这并不完全是你想要的(指针和引用),但它是有效的,这是一个对它有利的大点

------------对评论中问题的答复---------

不同之处在于,引用向量必须引用存在于其他地方的对象——管理这些对象的生命周期并确保它们比对它们的引用寿命更长的地方

智能指针向量必须指向堆分配的对象,并管理这些对象的生存期


这两种方法都有有效的用途。

boost::array
s构造函数正在尝试使用定义类型的默认构造函数初始化其所有成员,在本例中,该构造函数是一个没有默认构造函数的
boost::reference\u包装器,因为引用不能是未定义的。

reference\u包装器
s必须用引用的内容初始化。我用
std::vector
替换了
boost::array
,它现在可以工作了。它现在在将引用推入向量时创建引用。也许这是另一个问题,但我想知道你的看法。与您的方法相比,我不太确定这是好是坏。什么代表了更多的开销,保留向量还是
智能\u ptr
?我试图以注释的形式回答,但它太长了。我将编辑我的答案以添加回复。非常感谢。现在,我将采用我认为更具可读性的方法。如果表现不佳,我,呃。。。好吧,如果发生这种情况,我会想办法的。一个很好的方法!使其正确,使其可读,然后如果性能是一个问题,分析它并手动优化它。
C:/ide-4.6-workspace/chunkybacon/boost/boost/array.hpp:60: error: no matching function for call to 'boost::reference_wrapper<std::vector<int, std::allocator<int> > >::reference_wrapper()'
C:/ide-4.6-workspace/chunkybacon/boost/boost/ref.hpp:43: note: candidates are: boost::reference_wrapper<T>::reference_wrapper(T&) [with T = std::vector<int, std::allocator<int> >]
C:/ide-4.6-workspace/chunkybacon/boost/boost/ref.hpp:33: note:                 boost::reference_wrapper<std::vector<int, std::allocator<int> > >::reference_wrapper(const boost::reference_wrapper<std::vector<int, std::allocator<int> > >&)
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc: In constructor 'RefHolder::RefHolder()':
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc:6: note: synthesized method 'boost::array<boost::reference_wrapper<std::vector<int, std::allocator<int> > >, 3u>::array()' first required here 
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc: In function 'int main()':
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc:32: note: synthesized method 'RefHolder::RefHolder()' first required here
cc: C:/QNX641/host/win32/x86/usr/lib/gcc/i386-pc-nto-qnx6.4.0/4.3.3/cc1plus caught signal 1