Search code examples
c++c++11colorssfml

Cannot output red/green/blue color value of object


vector<Text> Game_Text;

Game_Text.push_back(Text("0",Game_Font[0],50));

cout<<Game_Text[0].getFillColor().r<<endl;

Using C++11 in Code::Blocks

Nothing it outputted when run, should it not output 255? If Game_Text[0].getFillColor().r is replaced with, say, "test", it outputs test as normal. No errors, full code is working.

Is it possible to output just a single r/g/b value of an object with this method?


Solution

  • The Color member r is of the type Uint8, which is an alias for unsigned char.

    And char (as well as signed char and unsigned char, and all aliases based upon these types) are handled as characters by the output operator <<.

    Therefore

    cout<<Game_Text[0].getFillColor().r<<endl;
    

    will attempt to print r as a character. If its value is not corresponding to a printable character, nothing will seem to be printed.

    To print the integer value you need to cast it to an integer-type that is not based on char:

    cout << static_cast<unsigned>(Game_Text[0].getFillColor().r) << '\n';