Problem:
An error message is encountered during compilation in Visual Studio 2015, stating "non-standard syntax; use '&' to create a pointer to member." This error pertains to the attempt to call a member function without using the proper pointer syntax.
Cause:
In C , member functions are accessed using pointers to member functions rather than directly. When using member functions within non-member functions, it's essential to prepend the member function name with an '&' to indicate the pointer.
Solution:
To resolve this error, modify the code to use the proper pointer syntax when calling member functions. Here's how you can fix the issue in your "player1Move" member function:
<code class="cpp">void TicTacToe::player1Move(string coordX) // ERROR { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; }</code>
The above code needs to be replaced with:
<code class="cpp">void TicTacToe::player1Move(string coordX) { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; }</code>
By adding '&' before the "player1Move" function name, you create a pointer to the member function, allowing you to call it correctly.
Additional Explanation:
When a member function is not called explicitly on an object instance (as a method in object-oriented programming), it can be called using a pointer to a member function. The '&' operator in C is used to create a pointer to a member function. This syntax adheres to the C standard and allows for proper access to member functions from non-member contexts.
The above is the detailed content of Why Am I Getting the \'non-standard syntax; use \'&\' to create a pointer to member\' Error in Visual Studio 2015?. For more information, please follow other related articles on the PHP Chinese website!