There is a button class that will be a button in the application. Used like this:

button playButton; playButton.create(20, 20, 120, 30, "123, 123, 123, "some", "value"); window->draw(playButton); 

The class itself is as follows:

 class button { public: void create(int x, int y, int width, int height, char color, char name, char text) { sf::CircleShape name(50); name.setFillColor(sf::Color(color)); name.setOutlineThickness(10); name.setOutlineColor(sf::Color(250, 150, 100)); name.setPosition(x, y); } private: }; 

However, when trying to build a program, the compiler spits out, producing:

  [Error] declaration of 'sf :: CircleShape name' shadows a parameter 

I just can not understand what's the matter. If you use it like this, without trying to shove it into a class, but putting it into the main thread, everything works fine:

 sf::CircleShape name(50); name.setFillColor(sf::Color(123,123,123)); name.setOutlineThickness(10); name.setOutlineColor(sf::Color(250, 150, 100)); name.setPosition(20, 20); window->draw(name); 
  • You have a function argument named name and you declare a local variable named name . - user194374
  • @kff comes out, is it not necessary to declare a local variable? - user1956243
  • Hard case ... - user194374

1 answer 1

In this definition of a member function of a class

 void create(int x, int y, int width, int height, char color, char name, char text) ^^^^^^^^^^ { sf::CircleShape name(50); ^^^^^^^^ 

declaring a local variable of type sf::CircleShape hides a parameter declaration of the same name with type char , which the compiler informs you of, since this definition of a function does not make sense.

In addition, you call a function by passing the string literals "some" and "value" to it as arguments "value"

 playButton.create(20, 20, 120, 30, "123, 123, 123, "some", "value"); ^^^^^^ ^^^^^^^ 

which, in general, if the corresponding parameters do not have reference types, are implicitly converted to pointers to their first characters and have the type const char * , but not char . That is, the compiler must still produce a diagnostic message about the wrong types of arguments for these parameters, that is, that it cannot convert, for example, the type const char[5] to the type char for the argument "some" .

Also note that the local variable name declared in the function body will become invalid after the function has completed. That is, the time of its life is the body of the function during its execution.

  • The TC also has a type mismatch when calling this devilry - const char* instead of char ... - user194374
  • @kff Thank you, I noted this discrepancy in my answer. - Vlad from Moscow