我正在尝试编写一个代码,检查answer2的颜色是否为蓝色,以确定用户是否得到了错误的答案,但编译器不允许在SFML中使用它
if (!next)
{
while (window.pollEvent(test))
{
switch (test.type)
{
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
if (Keyboard::isKeyPressed(Keyboard::Num1))
{
answer1.setFillColor(Color::Blue);
answer2.setFillColor(Color::Black);
}
if (Keyboard::isKeyPressed(Keyboard::Num2))
{
answer2.setFillColor(Color::Blue);
answer1.setFillColor(Color::Black);
}
if (Keyboard::isKeyPressed(Keyboard::Enter))
{
next = true;
running = true;
}
break;
}
}
}
next = false;
if (!next)
{
answer2.getFillColor = answer;
if (answer == "Blue")
{
question.setString("Correct");
}
else
{
question.setString("Wrong");
}
}
这是我从编译器获得的错误消息
c:\ users \只是一个人\ documents \ visual studio 2015 \ projects \ francais project \ francais project \ francais project.cpp(90):error C2659:'=':function as left operand
answer2.getFillColor = answer;
是错的。 getFillColor
是一个函数,你不能为它赋值。如果你想要该函数的值使用answer2.getFillColor()
并让它在表达式的右侧。
answer = answer2.getFillColor();
假设你的代码,answer
是一个std::string
。但你无法将sf::Color
转换为std::string
。相反,让answer
成为sf::Color
。通过这种方式,您可以通过与sf::Color::Blue
进行比较来检查它是否为蓝色:
sf::Color answer;
//...
answer = answer2.getFillColor();
if (answer == sf::Color::Blue)
{
question.setString("Correct");
}
else
{
question.setString("Wrong");
}