双矢量结构
structure of double vectors
本文关键字:结构 更新时间:2023-10-16
我正试图在C++中创建一个双向量结构。
struct distance{
vector<double> x(10000);
vector<double> y(10000);
vector<double> z(10000);
};
distance distance_old, distance_new;
在定义中,它抛出了一个错误:
error: expected identifier before numeric constant
error: expected ‘,’ or ‘...’ before numeric constant
我哪里错了?
我看过这篇文章向量C的结构++但它似乎对我不起作用。
您正试图构造结构中的向量,但这是无法完成的。你必须像普通类一样在构造函数中完成:
struct distance
{
vector<double> x;
vector<double> y;
vector<double> z;
distance()
: x(10000), y(10000), z(10000)
{ }
};
不能在结构声明中调用向量构造函数。去掉结构声明中的(10000)。如果你想使用非默认的向量构造函数来设置向量的初始容量,你需要在结构的构造函数中这样做。
一个打字错误,本质上——你需要
vector<double> x[10000];
...
括号错误!
此外,严格地说,您实际上定义的是一个向量数组,而不是双向量,即vector< vector<double> >
。根据你的目的,两者都可以。
EDIT:此解决方案使用g++编译并且没有运行时错误。
dist.h:
#include <vector>
using namespace std;
struct my_distance{
vector<double> x[10000];
vector<double> y[10000];
vector<double> z[10000];
};
dist.cpp:
#include "dist.h"
my_distance distance_old, distance_new;
int main()
{
return 0;
}
NB"distance"已经被STL用于其他用途,因此必须重命名。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?