WakeUpToFlatEarth WakeUpToFlatEarth - 1 year ago 56
C++ Question

sf::Text member function getFont() doesn't work?

In my program I have a function that creates an outline of another sf::Text string which is as follows:

void textDisplay::createOutline(std::vector<textDisplay> &textDisplayVector, std::vector<textDisplay> &textDisplayVector2, textDisplay &textDisplay2)
//create text2 npc dialogue outline
textDisplay2.text.setPosition(textDisplayVector.back().text.getPosition().x + 1, textDisplayVector.back().text.getPosition().y + 1);

I want to also copy the text's font, but i get this error:

'no suitable constructor exists to convert from "const sf::Font *" to "sf::Font"'.

I looked at the sfml documentation and I believe the issue lies with getFont() using a pointer rather than a reference. I have no idea how to implement this though. How can this commented out line of code that uses setFont() and getFont() be improved so that it works? All of the other lines of code work properly.

Answer Source

Like you suspect, the problem is that sf::Text::getFont returns a pointer to a Font, while sf::Text::setFont expects a reference to a Font as argument. Pointer != reference, therefore you get an error.

The solution is simple: Dereference (using the * operator) the pointer returned by getFont:

//                        ^
//                        |
// Notice the dereference operator here