其他分享
首页 > 其他分享> > c – 无法访问对向量中的值

c – 无法访问对向量中的值

作者:互联网

我有一个对矢量作为对象内的字段.所述对象具有一种方法,其中我需要访问向量中的对中的值.我使用迭代器指向我想要访问的向量中的位置.以下是包含向量的代码片段:

在头文件中:

vector<pair<double, double> > points;
vector<pair<double, double> >::iterator headingTo;

在构造函数中:

 points.push_back(make_pair(1700.00, 3300.00));//Plus 20 or so other values
 headingTo = points.begin();

在方法中:

double x = headingTo->first - positionX;
double y = headingTo->second - positionY;

但是,当我运行此代码时,没有创建y.当我使用断点来查看变量时,它根本没有在Visual Studio中显示.但是,如果我交换线条,y是可访问的,而x则不是.有任何想法吗?

编辑:
我发现了以下作品:

double headingToX = headingTo->first; 
headingToX -= positionX;
double headingToY = headingTo->second;
headingToY-= positionY;

解决方法:

检查您是否正在调试程序的优化版本 – 在这种情况下,如果编译器可以确定它不需要生成所需的程序输出,则可以自由地删除变量.

但是,即使您使用的是非优化版本,如果您根本不使用该变量,也会发生这种行为.在this bug report on the Express version of VC++ 2010中,您将看到Microsoft代表发表的以下评论(强调添加):

Is this issue only occuring on variable that you have not used in any
way other than assigning a value? The presence of the variable in the
.pdb via inspection with a hex-editor, or seeing a corresponding “mov”
instruction in the dissassembly does not guarantee that the compiler
did not do some level of optimzation that prevents the debugger from
inspecting the variable (the compiler always does small optimizations
even in a debug build). The debugger can only guarantee access to a
variable will be provided by the compiler if the variable is used in
the application for another purpose other than being assigned a value.

If you have a repro where the variable is being used (other than being
assigned a value) and you cannot inspect it in the debugger please let
us know. Otherwise, this is an artifact of compiler optimizations.

从您发布的代码中不清楚您是否稍后使用y的值.

标签:std-pair,c,vector
来源: https://codeday.me/bug/20190902/1789541.html