C++动态数组成员变量赋值
C++ Dynamic Array Member Variable Assignment
我在为动态 int 数组分配新值时遇到问题,该数组是 class IntersectionFlowRate() 的数据成员变量。我可以在构造函数中初始化并打印数组的值。但是,当我将构造函数退出到另一个 class 然后稍后调用 IntersectionFlowRate() class 中的函数时,传入变量以覆盖数据成员的初始值,它将出现分段错误。我调试发现覆盖数组导致段错误。并且即使尝试在其中一个函数中访问动态数组也会出现段错误。
我的问题是如何从其中一个函数中编辑动态 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
在.h文件中^
#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
}
我有另一个 class 创建一个指向 IntersectionFlowRate() 对象的指针,然后调用它的 setFlowCycle 函数传递两个 VALID 整数。通过调试,我能够很好地将 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
class:
的成员变量 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 中的一个对象成员。这会减少指针的使用:
class TrafficSim
{
public:
TrafficSim(); //Default Constructor
TrafficSim(const char* file); //Constructor
private:
IntersectionFlowRate m_flowRate;
};
然后:
TrafficSim::TrafficSim()
{
m_flowRate.setFlowCycle(0, 3);
}
关于如何在 class 中合并 std::vector
的用法的问题,这里是 IntersectionFlowRate
class 的代码示例,使用 vector
:
此外,问题的另一个来源是当您的 class 中有指向动态分配内存的指针时,您的 classes 无法遵循 Rule of 3。
使用 std::vector
会自动解决这个问题,但如果您坚持使用指针,则需要遵守发布的 link 中的说明。
是的,使用 std::vector,它更简单,而且它是一个模板,所以它也非常快并且适用于任何类型(最适合原始类型或对象指针),并且它还有边界检查和其他有用的东西。
如果您需要类似数组的快速访问,那么您可以使用 std::map 将键与值相关联,例如
std::map<UINT, YourClass*> m_mapIDs_to_YourClass;
当您第一次开始使用 stl 容器时,它们可能看起来有点奇怪,但不久之后您就离不开它们,幸运的是,它们已经成为 C++ 标准的一部分已有一段时间了。
这两个容器的边界检查可以通过将迭代器与 mapYourMap.end() 进行比较来完成,如果它们相等,则表示您已经传递了最后一个元素,并且尝试通过迭代器访问数据将导致异常。
std::vector 的示例(如果 vecInt 是一个 vector< int >):
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());
我在为动态 int 数组分配新值时遇到问题,该数组是 class IntersectionFlowRate() 的数据成员变量。我可以在构造函数中初始化并打印数组的值。但是,当我将构造函数退出到另一个 class 然后稍后调用 IntersectionFlowRate() class 中的函数时,传入变量以覆盖数据成员的初始值,它将出现分段错误。我调试发现覆盖数组导致段错误。并且即使尝试在其中一个函数中访问动态数组也会出现段错误。
我的问题是如何从其中一个函数中编辑动态 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
在.h文件中^
#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
}
我有另一个 class 创建一个指向 IntersectionFlowRate() 对象的指针,然后调用它的 setFlowCycle 函数传递两个 VALID 整数。通过调试,我能够很好地将 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
class:
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 中的一个对象成员。这会减少指针的使用:
class TrafficSim
{
public:
TrafficSim(); //Default Constructor
TrafficSim(const char* file); //Constructor
private:
IntersectionFlowRate m_flowRate;
};
然后:
TrafficSim::TrafficSim()
{
m_flowRate.setFlowCycle(0, 3);
}
关于如何在 class 中合并 std::vector
的用法的问题,这里是 IntersectionFlowRate
class 的代码示例,使用 vector
:
此外,问题的另一个来源是当您的 class 中有指向动态分配内存的指针时,您的 classes 无法遵循 Rule of 3。
使用 std::vector
会自动解决这个问题,但如果您坚持使用指针,则需要遵守发布的 link 中的说明。
是的,使用 std::vector,它更简单,而且它是一个模板,所以它也非常快并且适用于任何类型(最适合原始类型或对象指针),并且它还有边界检查和其他有用的东西。
如果您需要类似数组的快速访问,那么您可以使用 std::map 将键与值相关联,例如
std::map<UINT, YourClass*> m_mapIDs_to_YourClass;
当您第一次开始使用 stl 容器时,它们可能看起来有点奇怪,但不久之后您就离不开它们,幸运的是,它们已经成为 C++ 标准的一部分已有一段时间了。
这两个容器的边界检查可以通过将迭代器与 mapYourMap.end() 进行比较来完成,如果它们相等,则表示您已经传递了最后一个元素,并且尝试通过迭代器访问数据将导致异常。 std::vector 的示例(如果 vecInt 是一个 vector< int >):
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());