在我的课堂上,当我尝试将任何对象推送到向量 myCache 上时,我得到一个运行时错误。我知道我正在正确初始化向量,并且对为什么会发生这种情况感到困惑。
#ifndef CACHE_H
#define CACHE_H
#include <iostream>
#include "cacheblock.h"
using namespace std;
class Cache
{
public:
Cache(int rows, int numWords);
~Cache();
CacheBlock getBlock(int index);
private:
vector<CacheBlock> *myCache;
};
#endif
和
Cache::Cache(int rows, int numWords)
{
myCache = new vector<CacheBlock>;
CacheBlock test(rows, 0, 0);
myCache->push_back(test);
/*
for (int i = 1; i < rows; i++)
{
myCache->push_back(test);
cout << "inside loop\n\n";
}
*/
}
缓存块.h:
class CacheBlock
{
public:
CacheBlock(int numWords, int newIndex, int tagNum);
CacheBlock(const CacheBlock &newCacheBlock);
~CacheBlock();
void setData(int numWords, int newIndex, int tagNum);
private:
bool valid;
int index;
int tag;
vector<int> *dataWords;
};
任何人都可以帮忙吗?