【发布时间】:2013-12-12 11:47:06
【问题描述】:
我遇到的问题是我的向量超出范围。构造函数和另一个方法都在主函数中一个接一个地被调用,但问题是在构造函数运行后向量超出了范围。有人对如何解决这个问题有任何想法吗?我以为我修复了它,但只是让事情变得更糟。
标题:
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;
if (readWriteStatus == "read")
contents.writeStatus = true;
else if (readWriteStatus == "write")
contents.writeStatus = false;
contents.address = Addr;
contents.dirtyBit = false;
contents.statusBit = 0;
contents.indivBlockIndex = -1;
contents.indivBlockOffset = -1;
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 stuff not related to question
}
主要:
int main(int argc, char *argv[])
{
//string in = argv[1];
string inputfile = "C:/Users/Christopher/Downloads/testprac";
string infile = inputfile.append(".trace");
Cache myCache(infile);
// Parse Command Line Argument
// if(argc != 2)
// cout << "ERROR: Improper Number of Arguments" << endl;
// else
// {
int i = 1024, j = 8;
myCache.DirectMapped(i,j);
system ( "pause");
return 0;
}
主程序直接从myCache(infile)调用myCache.DirectMapped(i,j)连续两行。
感谢你们的帮助。对此,我真的非常感激。
【问题讨论】:
-
显示
main- 什么超出了范围,您为什么这么认为? -
“超出范围”是什么意思?
dataBase是一个成员对象,所以它不应该发生。结果你观察到了什么? -
一旦你告诉我们为什么你认为成员变量已经超出范围,我可能会想问
//other stuff not related to question -
@splrs 我观察到在构造函数的末尾,向量的大小是 3,然后在下一个方法的第一行运行之前它说向量的大小是一个巨大的数字.
-
“在第一行之前”是调试器经常无法显示任何正确值的地方(因为它们尚未设置)。向量更有可能不在 范围内。在得出任何结论之前先进入函数。
标签: c++ vector scope polymorphism