Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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++ 双[2]误差向量_C++_Arrays_Vector_Typedef - Fatal编程技术网

C++ 双[2]误差向量

C++ 双[2]误差向量,c++,arrays,vector,typedef,C++,Arrays,Vector,Typedef,为什么会出现此错误: #include <vector> typedef double point[2]; int main() { std::vector<point> x; } #包括 typedef双点[2]; int main() { std::向量x; } /usr/include/c++/4.3/bits/stl_construct.h:在函数“void std::_Destroy(_Tp*)[with _Tp=double[2]]中: /us

为什么会出现此错误:

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

int main()
{
     std::vector<point> x;
}
#包括
typedef双点[2];
int main()
{
std::向量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)[带_ForwardIterator=double(*)[2]]实例化 /usr/include/c++/4.3/bits/stl_construct.h:128:从“void std::_Destroy(_ForwardIterator,_ForwardIterator,std::allocator&)[带_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]'
如何解决?

我看这里没有问题。也许更新的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$ 

你能解决这个问题的唯一方法就是停止做你想做的事。数组不可复制或分配

老实说,我甚至不知道你能做这样的事。看来编译器基本上是疯了。这并不让我感到惊讶。我不知道确切的原因,但我知道这根本不会起作用

另一方面,您应该能够毫不费力地包含boost::array

typedef boost::array<double,2> point;
typedef boost::数组点;

您应该查看文档以确保我是正确的,但我非常确定此类型是可分配的,并且可以复制构造的。

使用结构或静态数组类(如boost::array)来包含double。

您不能这样做。如前所述,数组不可复制或分配,这是
std::vector
的要求。我建议:

#include <vector>
struct point {
    double x;
    double y;
};

int main() {
     std::vector<point> v;
}

更明显的是,向量包含点(坐标?

为了给出另一种解决方案,您还可以使用一对双精度:

#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));
}
#包括
#包括
typedef std::对点;
int main()
{
std::向量x;
x、 推回(标准::制作配对(3.0,4.0));
}

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

我不会投反对票,但代码有问题。也就是说,数组不可复制/分配,这是
std::vector
的要求;当然不是GCCAt,有一次,在我有线索之前,我常常把引用放在std::vector中,GCC把它们吃光了。我认为实现不会阻止你,直到它实际必须使用你没有提供的概念的任何部分。@Noah:是的,很多错误只能在模板实例化过程中被捕获。
put(v[0].x, v[0].y, value);
#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));
}