【问题标题】:error: C2661: 'std::array<uint,3>::array': no overloaded function takes 3 arguments错误:C2661:'std::array<uint,3>::array':没有重载函数需要 3 个参数
【发布时间】:2021-03-19 12:07:39
【问题描述】:

我收到此错误:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xmemory0:881: 错误:C2661: 'std::array ::array': 没有重载函数需要 3 个参数

在这行代码处:

    template<class _Objty,
        class... _Types>
        static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
        {   // construct _Objty(_Types...) at _Ptr
        ::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
            _Objty(_STD forward<_Types>(_Args)...);          // ** Error is here
        }

我正在像这样使用std::array&lt;uint, 3&gt;

void MyClass::myMethod(
        // ...
        , std::vector<std::array<uint, 3>> &indices
        // ...
        , const size_t ssteps
        // ...
        )
{
    // ...
    
    indices.reserve(2*ssteps);
    auto steps = int(ssteps);
    auto offs = steps;
    auto last = steps - 1;

    // ...

    indices.emplace_back(0, last, offs);
    indices.emplace_back(last, offs + last, offs);

    // ...
}

我不明白为什么在 xmemory0 包含的文件中会发生此错误。任何提示表示赞赏。

【问题讨论】:

  • std::array 没有像这样接受 3 个参数的构造函数,你打算做什么?
  • indices.emplace_back(0, last, offs) 调用构造函数 array&lt;uint, 3&gt;::array(0, last, offs)array doesn't have such constructor;使用indices.push_back({0, last, offs}) 代替它。

标签: c++ std stdarray


【解决方案1】:

正如@kakkoko 已经指出的那样:

indices.emplace_back(0, last, offs) 调用构造函数 array&lt;uint, 3&gt;::array(0, last, offs)array doesn't have such constructor;

不幸的是,额外的一对花括号是不够的:

  indices.emplace_back({ 0u, last, offs }); // Doesn't work! :-(

输出:

main.cpp: In function 'int main()':
main.cpp:11:42: error: no matching function for call to 'std::vector<std::array<unsigned int, 3> >::emplace_back(<brace-enclosed initializer list>)'
   11 |   indices.emplace_back({ 0u, last, offs });
      |                                          ^
In file included from /usr/local/include/c++/10.2.0/vector:72,
                 from main.cpp:2:
/usr/local/include/c++/10.2.0/bits/vector.tcc:109:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = std::array<unsigned int, 3>; _Alloc = std::allocator<std::array<unsigned int, 3> >; std::vector<_Tp, _Alloc>::reference = std::array<unsigned int, 3>&]'
  109 |       vector<_Tp, _Alloc>::
      |       ^~~~~~~~~~~~~~~~~~~
/usr/local/include/c++/10.2.0/bits/vector.tcc:109:7: note:   candidate expects 0 arguments, 1 provided

Live Demo on coliru

因此,必须明确构造元素:

  indices.emplace_back(std::array<uint, 3>{ 0u, last, offs }); // Does work. :-)

Live Demo on coliru

std::vector::push_back() 必须改为:

  indices.push_back(std::array<uint, 3>{ 0u, last, offs });

Live Demo on coliru


一个完整的演示:

#include <array>
#include <iostream>
#include <vector>

using uint = unsigned;

int main()
{
  std::vector<std::array<uint, 3>> indices;
  const uint last = 3, offs = 4;
  
  indices.emplace_back(std::array<uint, 3>{ 0u, last, offs });
  indices.push_back({ last, offs + last, offs });
  
  std::cout << "indices: {";
  const char *sep = "\n";
  for (const std::array<uint, 3> &indicesI : indices) {
    std::cout << sep << "  {";
    const char *sepI = " ";
    for (const uint i : indicesI) {
      std::cout << sepI << i;
      sepI = ", ";
    }
    std::cout << " }";
    sep = ",\n";
  }
  std::cout << "\n}\n";
}

输出:

indices: {
  { 0, 3, 4 },
  { 3, 7, 4 }
}

Live Demo on coliru

【讨论】:

  • 谢谢 =) 为了完整起见,indices.push_back({0, last, offs}); 也解决了错误!
  • @user3405291 很好。我编辑了我的答案。
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
相关资源
最近更新 更多