如何在动态二维数组中存储数据,在结构体中声明,该结构体也被c++中的另一个结构体使用
How to store data in a dynamic 2D array, declared inside a struct which is also used by another struct in C++
我正试图调试别人的代码,我不能从文件中读取数据到一个变量,在头文件中声明。我从。c和。h文件附加的片段。
//c#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include "test.H"
int main()
{
Initialize();
return(0);
}
//test.C。
#include <fstream>
#include <utility>
#include <iostream>
#include <string>
#include <cstring>
#include "test.H"
#include "BB.H"
using namespace std;
#define file_to_be_read "file_to_be_read"
int NUM[10] = {3,2,3,4,5,2,3,1,3,4};
OBJECTS * OBJECTS::m_list[10];
void OBJECTS::createObjects()
{
//cout << "Print somethin'" << endl;
OBJECTS * objs = new OBJECTS(A_size, NUM[A]);
m_list[A] = objs;
objs = new OBJECTS(B_size, NUM[B]);
m_list[B] = objs;
objs = new OBJECTS(C_size, NUM[C]);
m_list[C] = objs;
objs = new OBJECTS(D_size, NUM[D]);
m_list[D] = objs;
objs = new OBJECTS(E_size, NUM[E]);
m_list[E] = objs;
objs = new OBJECTS(F_size, NUM[F]);
m_list[F] = objs;
objs = new OBJECTS(G_size, NUM[G]);
m_list[G] = objs;
objs = new OBJECTS(H_size, NUM[H]);
m_list[H] = objs;
objs = new OBJECTS(I_size, NUM[I]);
m_list[I] = objs;
objs = new OBJECTS(J_size, NUM[J]);
m_list[J] = objs;
char name[100];
sprintf(name,file_to_be_read);
ifstream fin(name);
// Load the file.
string buf;
for (int i = 0; i < 10; i++)
{
objs = m_list[i];
for (int j = 0; j < NUM[i]; j++)
{
fin >> buf; // read separator line.
cout << buf << endl;
for (int k = 0; k < objs->size_m; k++)
{
fin >> buf;
cout << buf << endl;
fin >> objs->sub_objs[j].coords[k][0]; // Segmentation error
fin >> objs->sub_objs[j].coords[k][1];
}
}
}
}
void Initialize()
{
OBJECTS::createObjects();
}
test.H文件#ifndef _test_H
#define _test_H
#include <cmath>
#include <cassert>
#include <vector>
#define A 0
#define B 1
#define C 2
#define D 3
#define E 4
#define F 5
#define G 6
#define H 7
#define I 8
#define J 9
extern int NUM[];
//////////////////////
struct SUBOBJECTS
{
SUBOBJECTS(int size)
{
coords = new double[size][2];
}
double (*coords)[2];
};
//////////////////////
struct OBJECTS
{
OBJECTS(int size, int number) :
size_m(size), num_m(number)
{
std::vector<SUBOBJECTS> sub_objs(number,size);
}
static void createObjects();
static OBJECTS * m_list[10];
SUBOBJECTS * sub_objs;
int size_m;
int num_m;
};
void Initialize();
#endif
//BB.H const int A_size = 2;
const int A_size = 2;
const int B_size = 3;
const int C_size = 3;
const int D_size = 2;
const int E_size = 2;
const int F_size = 1;
const int G_size = 1;
const int H_size = 3;
const int I_size = 3;
const int J_size = 2;
file_to_be_read
#######################
0.658 1.291
1.891 1.753
#----------------------
2.570 2.955
3.747 3.417
#----------------------
3.658 4.141
5.096 2.800
#######################
5.293 1.842
4.518 1.511
6.215 1.450
#----------------------
6.113 3.239
5.960 3.966
7.033 2.842
#######################
0.658 1.291
1.919 1.720
2.404 2.998
#----------------------
3.674 3.536
4.073 3.004
4.379 4.781
#----------------------
3.915 5.534
3.063 5.270
4.417 6.366
#######################
5.500 5.125
5.850 4.551
#----------------------
5.997 5.958
0.658 1.291
#----------------------
1.899 1.698
2.455 3.008
#----------------------
3.751 3.337
4.562 3.037
#######################
3.962 4.032
2.945 4.456
#----------------------
1.997 4.312
3.125 4.921
#----------------------
5.212 4.220
5.982 3.896
#----------------------
5.386 4.685
0.658 1.291
#----------------------
1.873 1.711
2.481 2.999
#######################
1.556 4.164
#----------
0.658 1.291
#######################
1.891 1.753
#----------------------
0.658 1.291
#----------------------
1.919 1.720
#######################
0.658 1.291
1.841 1.793
1.894 1.773
#######################
0.658 1.291
1.894 1.773
2.486 3.006
#----------------------
3.032 2.690
3.045 1.710
3.546 3.682
#----------------------
3.561 4.989
3.219 5.337
3.915 5.627
#######################
3.995 3.220
3.982 2.227
#---------------------
4.348 3.863
1.467 4.890
#---------------------
1.467 4.890
2.201 4.539
#---------------------
2.842 3.762
2.115 5.051
我知道sub_objs
的定义有问题。但我不知道是什么。为了在公共论坛上发布,我不得不稍微修改变量和代码。请记住与此有关的任何错误并提供帮助!
在OBJECTS
构造函数中有以下行:
std::vector<SUBOBJECTS> sub_objs(number,size);
声明了一个名为sub_objs
的局部变量。一旦析构函数退出,这个变量就会超出作用域。它也与OBJECTS::sub_objs
成员变量没有任何关系。
所以你从来没有在任何地方初始化OBJECTS::sub_objs
成员变量,这意味着它的值将是不确定的,以任何方式使用它都会导致未定义的行为和你看到的崩溃。
有两个解决方案:
由于成员变量是一个指针,您需要使用
new[]
为它分配内存。将成员变量的类型改为vector,并在
OBJECTS
构造函数初始化列表中构造。
这部分是做什么的呢?
struct SUBOBJECTS
{
SUBOBJECTS(int size)
{
coords = new double[size][2]; // [2]
}
double (*coords)[3]; // [3]
};
声明了一个包含3个双精度体的数组,然后创建了一个包含2个双精度体的数组。这可能会导致严重的问题。
num
的size为10,则分配20个双床空间,但声明为30个双床空间。
相关文章:
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 包含 std::list 的结构体的 C++ 初始化
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何使用结构体的向量数组?
- 如何使用结构体在C++中更改这些代码?
- 无法在 Mosquitto MQTT Broker 插件上访问结构体 mosquitto 的元素
- 我应该如何在C++中使用结构体解决输入失败的问题?
- Qsort() 比较结构体整数的总和
- 如何使用迭代器指向结构体c++的向量
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 如何获取结构体成员的地址
- 创建结构体向量,表达式:向量下标超出范围
- boost::任何带有结构体和无符号整数
- 如何在构造函数中初始化结构体的动态数组?
- 只写结构体的某些字段
- C++ push_back() 一个结构体到一个向量中
- C++14 结构体上的统一初始化
- 访问不同类的私有结构体成员