【问题标题】:vector of double[2] errordouble[2] 错误向量
【发布时间】:2011-03-07 01:29:37
【问题描述】:

为什么会出现这个错误:

#include <vector>
typedef double point[2];

int main()
{
     std::vector<point> x;
}
/usr/include/c++/4.3/bits/stl_construct.h:在函数“void std::_Destroy(_Tp*) [with _Tp = double [2]]”中: /usr/include/c++/4.3/bits/stl_construct.h:103:从'void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = double (*)[2]]'实例化 /usr/include/c++/4.3/bits/stl_construct.h:128: 从 'void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator&) [with _ForwardIterator = double (*)[2], _Tp = double [2]]' /usr/include/c++/4.3/bits/stl_vector.h:300:从“std::vector::~vector() [with _Tp = double [2], _Alloc = std::allocator]”实例化 prova.cpp:8:从这里实例化 /usr/include/c++/4.3/bits/stl_construct.h:88: 错误:在‘* __pointer’中请求成员‘~double [2]’,它是非类类型‘double [2]’

如何解决?

【问题讨论】:

    标签: c++ arrays vector typedef


    【解决方案1】:

    你不能那样做。如前所述,数组不可复制或分配,这是std::vector 的要求。我会推荐这个:

    #include <vector>
    struct point {
        double x;
        double y;
    };
    
    int main() {
         std::vector<point> v;
    }
    

    无论如何它会更好读,因为您可以执行以下操作:

    put(v[0].x, v[0].y, value);
    

    这使得向量包含点(坐标?)更加明显

    【讨论】:

      【解决方案2】:

      解决此问题的唯一方法是停止尝试做您想做的事情。数组不可复制或分配。

      老实说,我什至不知道你可以尝试做这样的事情。看来编译器基本上是吓坏了。这并不让我感到惊讶。我不知道具体原因,但我知道这根本行不通。

      另一方面,您应该能够毫无困难地包含 boost::array。

      typedef boost::array<double,2> point;
      

      您应该查看文档以确保我是正确的,但我很确定这种类型是可分配和可复制构造的。

      【讨论】:

        【解决方案3】:

        只是给出一个替代的解决方案,你也可以使用一对双:

        #include <vector>
        #include <utility>
        
        typedef std::pair<double, double> point;
        
        int main()
        {
            std::vector<point> x;
            x.push_back(std::make_pair(3.0, 4.0));
        }
        

        但名为 point 的结构或类可能是最好的解决方案。

        【讨论】:

          【解决方案4】:

          我认为这里没有问题。也许更新的 GCC (Glibc) 会解决这个问题?

              shade@vostro:~$ ls /usr/include/c++/
              4.4  4.4.3
              shade@vostro:~$ cd ~/Desktop
              shade@vostro:~/Desktop$ g++ test.cpp 
              shade@vostro:~/Desktop$ ./a.out 
              shade@vostro:~/Desktop$ 
          

          【讨论】:

          • 我不会投反对票,但代码有问题。即数组不可复制/可分配,这是std::vector 的要求。
          • 我看不到任何编译器编译它;当然不是海合会
          • 有一次,在我没有任何线索之前,我曾经将引用放在 std::vector 中,而 GCC 将它们吃光了。我认为实现根本不会阻止你,直到它实际上必须使用你不提供的任何需要的概念部分。
          • @Noah:是的,很多错误只能在模板实例化过程中被捕获。
          【解决方案5】:

          使用诸如 boost::array 之类的结构或静态数组类来包含双精度。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-31
            • 1970-01-01
            • 2020-02-19
            • 1970-01-01
            • 2016-10-21
            • 1970-01-01
            相关资源
            最近更新 更多