我的for循环被跳过,尽管调试器显示它不应该。

huangapple go评论51阅读模式
英文:

My for-loop is getting skipped, even though the debugger says it shouldn't

问题

Somehow, my for-loop (the first one) keeps getting skipped. even after stepping through it with the debugger, i cannot figure out why.

Grid AssetManager::deserializeGrid(std::string const& filename)
{
	std::ifstream rf(filename, std::ios::binary);
	if (!rf)
		std::cout << "Cannot open file!" << std::endl;
	//reading size of grid
	int gx, gy;
	rf.read((char*)&gx, sizeof(int));
	rf.read((char*)&gy, sizeof(int));
	Grid rGrid(gx, gy);
	for (int x = 0; x < gx; x++) 
	{
		//Deserializing code here
	}
	rf.close();
	if (!rf.good())
		std::cout << "Error occurred at reading time!" << std::endl;
	return rGrid;
}

I'm trying to read a saved file here.

While debugging, I see that gx and gy are successfully read and have a value of 4. Somehow though, the for-loop gets skipped and it jumps straight to the return statement.
Edit:
I've added 3 couts: one immediately in front of the declaration of rGrid, one directly after, and one before the return statement. Only the first gives me an output (verifying that gx and gy are indeed 4). The other two are also getting skipped. It jumps from rGrid directly to the return statement. In the constructor of rGrid, a 2d vector of the size gx, gy gets created, nothing else (I've also checked this and it works without error).

英文:

Somehow, my for-loop (the first one) keeps getting skipped. even after stepping through it with the debugger, i cannot figure out why.

Grid AssetManager::deserializeGrid(std::string const& filename)
{
	std::ifstream rf(filename, std::ios::binary);
	if (!rf)
		std::cout << "Cannot open file!" << std::endl;
	//reading size of grid
	int gx, gy;
	rf.read((char*)&gx, sizeof(int));
	rf.read((char*)&gy, sizeof(int));
	Grid rGrid(gx, gy);
	for (int x = 0; x < gx; x++) 
	{
		//Deserializing code here
	}
	rf.close();
	if (!rf.good())
		std::cout << "Error occurred at reading time!" << std::endl;
	return rGrid;
}

I'm trying to read a saved file here.

While debugging, I see that gx and gy are succesfully read and have a value of 4. Somehow though, the for-loop gets skipped and it jumps straight to the return statement.
Edit:
I've added 3 couts: one immediately infront of the declaration of rGrid, one directly after and one before the return statement. Only the first gives me an output (verifying that gx and gy are indeed 4). The other two are also getting skipped. It jumps from rGrid directly to the return statement. In the constructor of rGrid, a 2d vector of the size gx, gy gets created, nothing else (I've also checked this and it works without error).

答案1

得分: 0

我发现了答案(部分?)。我意识到 VisualStudio 在调试时跳过了所有的代码,直接跳到返回语句,而实际上没有返回任何东西,然后再返回以完成代码。所以它似乎只是跳过了代码,但实际上并没有。我感到相当愚蠢,没有在很长时间内意识到这一点。我不知道为什么 VisualStudio 这样做(不仅仅是在我的电脑上),但嘿 - 至少现在它可以工作了。

英文:

Well, I found the answer (kinda?). I realized that VisualStudio skipped all of the code (when debugging) and jumped to the return statement without actually returning anything and then going back to finish the code. So it only seemed to skip code but it really wasn't. I feel quite stupid not realising this for way too long. I don't know why VisualStudio is doing this (not only on my PC), but hey - at least it's working now.

huangapple
  • 本文由 发表于 2023年5月15日 04:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定