功能不按预期工作? [英] Function not working as intended?
问题描述
p>
我写了一个函数来设置sf :: Text对象的参数:
void FontParam(sf :: Text obj,sf :: Font font,std :: string text,sf :: Color color,int size,int positionX,int positionY)
{
obj.setCharacterSize (尺寸);
obj.setFont(font);
obj.setColor(color);
obj.setString(text);
obj.setPosition(positionX,positionY);
$ / code>
使用:
FontParam(T [0],font_Clubland, R,SF ::颜色::白色,100,100,100);
并绘制它将不起作用。写作:
t [0] .setFont(font_Clubland);
t [0] .setColor(sf :: Color :: White);
t [0] .setPosition(100,100);
t [0] .setString(R);
t [0] .setCharacterSize(100);
并绘制它。
帮助或提示非常感谢,提前致谢!
编辑:--------------------- ------
有人想知道,访问冲突发生是因为我没有引用sf :: Font。正确的功能是:
void FontParam(sf :: Text& obj,sf :: Font& font,std :: string text,sf :: Color color,int size,int positionX,int positionY)
{
obj.setCharacterSize(size);
obj.setFont(font);
obj.setColor(color);
obj.setString(text);
obj.setPosition(positionX,positionY);
$ / code>
感谢您的帮助!您正在向您的函数传递 t [0]
的副本,更改复制,然后放弃更改。相反,您可能希望通过引用来传递 t [0]
,以便更改更新为原始对象。通过将参数 sf :: Text obj
更改为 sf :: Text& obj
(注意添加了&
)。
First off I'd like to say I'm still a complete novice when it comes to Programming, and I've already searched for an answer to this problem, thing is, I'm not sure what's wrong.
I wrote a function to set parameters for an sf::Text object:
void FontParam(sf::Text obj, sf::Font font, std::string text, sf::Color color, int size, int positionX, int positionY)
{
obj.setCharacterSize(size);
obj.setFont(font);
obj.setColor(color);
obj.setString(text);
obj.setPosition(positionX,positionY);
}
Using:
FontParam(t[0],font_Clubland,"R",sf::Color::White,100,100,100);
and drawing it will not work. Writing it as:
t[0].setFont(font_Clubland);
t[0].setColor(sf::Color::White);
t[0].setPosition(100,100);
t[0].setString("R");
t[0].setCharacterSize(100);
and drawing it will.
Any help or tips are very much appreciated, thanks in advance!
EDIT: ---------------------------
Incase anyone was wondering, the access violation was happening because I wasn't referencing the sf::Font. The correct function would be:
void FontParam(sf::Text &obj, sf::Font &font, std::string text, sf::Color color, int size, int positionX, int positionY)
{
obj.setCharacterSize(size);
obj.setFont(font);
obj.setColor(color);
obj.setString(text);
obj.setPosition(positionX,positionY);
}
Thanks for the help!
You're passing a copy of t[0]
into your function, changing the copy, and then discarding the changes. Instead, you probably want to pass your t[0]
by reference so that the changes update into the original object. This is easily done by changing the parameter sf::Text obj
to sf::Text& obj
(Note the &
added).
这篇关于功能不按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!