Home > Article > Backend Development > Why is Declaring Multiple Object Pointers on a Single Line In C Error-Prone?
Declaring Multiple Object Pointers on One Line
In this code snippet, a developer encountered an error when declaring multiple object pointers on a single line.
<code class="cpp">private: sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;</code>
This declaration resulted in a compiler error stating that there was no match for the assignment operator. The error occurred because the line as written only declares a single pointer, re_sprite_hair, and two non-pointer objects, re_sprite_body and re_sprite_eyes.
To resolve this issue and create three valid object pointers, each variable must be prefixed with its own asterisk (*):
<code class="cpp">private: sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes;</code>
This revised declaration allocates three separate pointers, allowing for proper storage and manipulation of three distinct objects. It is important to remember that when declaring multiple object pointers on one line, each variable should have its own asterisk to indicate that it is a pointer variable.
The above is the detailed content of Why is Declaring Multiple Object Pointers on a Single Line In C Error-Prone?. For more information, please follow other related articles on the PHP Chinese website!