功能不按预期工作? [英] Function not working as intended?

查看:184
本文介绍了功能不按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说的是,在编程方面我仍然是一个完全的新手,而且我已经在寻找这个问题的答案,事实上,我不确定是哪里出了问题。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆