【发布时间】:2015-05-26 02:36:09
【问题描述】:
当我尝试用 c++ 编译这个源代码时:
void ParticleSystem::setState(std::vector<Vec2f>& statesVector)
{
std::vector<Vec2f> pState(2);
for (int i = 0; i < 2*np; i += 2) {
pState[0] = *statesVector[i];
pState[1] = *(statesVector[i+1]);
(*particles[i/2]).setState(pState);
}
}
我收到以下错误:
ParticleSystem.cpp:110:15: error: use of overloaded operator '*' is ambiguous (operand type
'value_type' (aka 'gfx::TVec2<float>'))
pState[0] = *statesVector[i];
^~~~~~~~~~~~~~~~
ParticleSystem.cpp:110:15: note: built-in candidate operator*(float *)
ParticleSystem.cpp:110:15: note: built-in candidate operator*(const float *)
ParticleSystem.cpp:111:15: error: use of overloaded operator '*' is ambiguous (operand type
'value_type' (aka 'gfx::TVec2<float>'))
pState[1] = *(statesVector[i+1]);
我已经在论坛中查找错误并执行了一些步骤,但我始终无法使其正常工作。此外,我也试图理解错误注释中的解释,但我不能。 我真的希望有人可以帮助我。
最后,如果有人对这个问题给予负面评价,请至少解释原因。
【问题讨论】:
-
Vec2f是函数指针吗? -
statesVector[i]计算结果为Vec2f&。没有为Vec2f定义一元运算符*。目前还不清楚你打算在这条线上做什么。 -
而且,
operator []拥有 higher precedence 而不是operator *无论如何。您可能应该首先区分您真正想要做什么。请指出在您的问题中Vec2f实际上是什么。有许多使用该名称声明类型的工具包,但您没有具体说明您正在使用哪种类型。 real MCVE 会极大地帮助。 -
如果您只想影响
statesVector的值,则不需要*。引用不需要*运算符来获取值。 -
@meneldal ,这就是答案。谢谢大家!
标签: c++ pointers vector reference