矢量超出范围
Vector going out of scope
本文关键字:范围 更新时间:2023-10-16
所以我的结构向量超出范围时遇到了问题。我不知道为什么。我以前使用过矢量,但由于某种原因,这不起作用。我看了几个论坛,尝试了几种不同的方法来解决这个问题。
构造函数和DirectMap方法都在main中被调用。我希望向量保持在两种方法的作用域中。这可能吗?感谢Alot的帮助!
以下是我在头文件中声明的方式:
struct input
{
bool dirtyBit;
int statusBit; //0 not in cache, 1 in cache, 2 in 2nd cache
bool writeStatus; //write = 1 read = 0
int address;
int indivBlockIndex;
int indivBlockOffset;
};
class Cache
{
public:
vector<input *> dataBase;
Cache(string);
~Cache();
void DirectMapped(int, int);
};
以下是我如何使用它:
Cache::Cache(string infile)
{
ifstream in(infile);
string readWriteStatus;
int Addr;
while (in >> readWriteStatus >> hex >> Addr)
{
input *contents;
contents = new input;
contents->dirtyBit = false;
contents->statusBit = 0;
dataBase.push_back(contents);
}
}
Cache::~Cache(){}
void Cache::DirectMapped(int cacheSize, int blockSize)
{
//initial stats needed
int blockCount = cacheSize/blockSize; //number of total blocks
//clear out the cache
for (int i = 0; i <= dataBase.size(); i++)
dataBase[i]->statusBit = 0;
//other irrelevent stuff after this
}
再次感谢你抽出时间来帮助我。
问题是,您在向量中存储指向input
结构的指针,但从未使它们指向任何位置。一个简单的解决方案可能是更改
input *contents;
至
input *contents = new input;
然而,管理一个原始点向量会产生各种令人讨厌的后果。在您的特定情况下,我认为没有理由首先保留指针向量。只需使用
vector<input> dataBase;
并存储实际结构。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 如何访问超出其块范围的对象?
- 矢量下标超出SFML游戏中的范围