动态数组成员变量赋值
C++ Dynamic Array Member Variable Assignment
我有一个问题,分配新的值给一个动态int数组,这是类IntersectionFlowRate()的数据成员变量。我可以在构造函数中初始化和打印数组的值。但是,当我退出构造函数到另一个类,然后稍后调用IntersectionFlowRate()类中的函数传递变量来覆盖数据成员的初始值时,它将分段错误。我进行了调试,发现重写数组会导致segfault。即使试图访问动态数组中的一个函数也会出现故障。
我的问题是如何编辑一个动态int数组成员变量的值从它的函数之一,即setArrayElement(int index, int x)。
下面是我的一些代码。抱歉,如果我不清楚或错过了一些荒谬的东西。这件事我已经纠结了好几个小时了。 #ifndef INTERSECTIONFLOWRATE_H
#define INTERSECTIONFLOWRATE_H
class IntersectionFlowRate
{
public:
IntersectionFlowRate();
~IntersectionFlowRate();
void setFlowCycle(int index, int flow);
private:
int* m_flowRateMotorCycle;
};
#endif
^
#include "IntersectionFlowRate.h"
#include <cstdlib>
#include <iostream>
#include <new>
using namespace std;
IntersectionFlowRate::IntersectionFlowRate()
{
const int SIZE = 4; //Constant for m_flowRates[] size
//DYNAMIC MEMORY DELETE LATER
m_flowRateMotorCycle = new int[SIZE];
for(int i = 0; i < SIZE; i++){
m_flowRateMotorCycle[i] = 0;
cout << m_flowRateMotorCycle[i] << endl;
cout << "WE GOT HERE" << endl;
}
}
void IntersectionFlowRate::setFlowCycle(int index, int flow){
cout << "INDEX: " << index << endl;
cout << "FLOW: " << flow << endl;
m_flowRateMotorCycle[index] = flow; //seg fault is here
}
我有另一个类,创建一个指向IntersectionFlowRate()对象的指针,然后调用它的setFlowCycle函数传递两个VALID int。通过调试,我能够将0和3传递给函数setFlowCycle(0,3),并在函数中输出这些变量。
#ifndef TRAFFICSIM_H
#define TRAFFICSIM_H
#include "IntersectionFlowRate.h"
using namespace std;
class TrafficSim
{
public:
TrafficSim(); //Default Constructor
TrafficSim(const char* file); //Constructor
~TrafficSim(); //Destructor
private:
IntersectionFlowRate* m_flowRate;
};
#endif
#include "TrafficSim.h"
#include "IntersectionFlowRate.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
TrafficSim::TrafficSim()
{
IntersectionFlowRate* m_flowRate = new IntersectionFlowRate();
m_flowRate->setFlowCycle(0, 3);
}
我用这段代码重复了这个错误。如果没有其他人可以,我完全不确定哪里可能出了问题。
您正在设置一个名为m_flowRate
的局部变量,而不是TrafficSim
类的成员变量m_flowRate
:
而不是:
TrafficSim::TrafficSim()
{
IntersectionFlowRate* m_flowRate = new IntersectionFlowRate();
m_flowRate->setFlowCycle(0, 3);
}
应该是这样的:
TrafficSim::TrafficSim()
{
m_flowRate = new IntersectionFlowRate();
m_flowRate->setFlowCycle(0, 3);
}
但总的来说,它不需要是指针。它可以是类中的对象成员。这将减少一点指针的使用:
class TrafficSim
{
public:
TrafficSim(); //Default Constructor
TrafficSim(const char* file); //Constructor
private:
IntersectionFlowRate m_flowRate;
};
:
TrafficSim::TrafficSim()
{
m_flowRate.setFlowCycle(0, 3);
}
关于如何在类中合并使用std::vector
的问题,这里是IntersectionFlowRate
类的代码示例,使用vector
重写:
向量样本
同样,问题的另一个来源是,当您在类中有指向动态分配内存的指针时,您的类不能遵循规则3。
使用std::vector
自动处理此问题,但如果您坚持使用指针,则需要遵守所发布的链接中的说明。
是的,使用std::vector,它更简单,它是一个模板,所以它也非常快,可以工作任何类型(最适合原始类型或对象指针),它也有边界检查和其他有用的东西。
如果你需要快速的类似数组的访问,那么你可以使用std::map,它将键与值相关联,就像这样
std::map<UINT, YourClass*> m_mapIDs_to_YourClass;
当你第一次开始使用stl容器时,它们可能看起来有点奇怪,但不久之后你就离不开它们了,幸运的是,它们现在已经成为c++标准的一部分有一段时间了。
这两个容器的边界检查可以通过比较你的迭代器和mapYourMap.end()来完成,如果它们相等,你已经传递了最后一个元素,并且试图通过迭代器访问数据将导致异常。std::vector的例子(如果vecInt是一个vector
vector<int>::iterator it = vecInt.begind();
if (it == vecInt.end()) return; // vector is empty
do { // runs through elememts until out of bound, useful for searching
i++
while (it != vecInt.end());
- 为"adjacent"变量赋值时出现问题
- 非常量变量只读位置的赋值
- 变量在使用赋值语句赋值后恢复为以前的值
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 如何在 c++ 中正确声明/赋值变量的值
- 在未初始化的变量上使用复合赋值运算符(+=, ..)不是C++中的UB?
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 如何在不使用赋值运算符的情况下为动态变量赋值?
- 为什么我不能为变量赋值函数?
- 可视化的c++变量赋值使代码正常工作
- 作为赋值(增加引用变量)C++的左操作数所需的左值
- 如何为结构类型变量赋值
- const变量是否可以在具有默认值的参数中赋值(作为可选参数)
- C++中重载复合赋值运算符不会更改变量
- 初始值设定项列表与构造函数赋值与变量定义
- 在结构的构造函数中使用类方法赋值变量
- 原子变量的多重赋值是原子操作吗?
- 在 If 语句 [C++] 中赋值变量
- 赋值变量时c++未处理异常访问冲突
- 使用reinterpret_cast赋值变量后可能的析构函数