Home > Article > Backend Development > How to Resolve \"non-standard syntax; use \'&\' to create a pointer to member\" Error in Visual Studio 2015?
Visual Studio 2015 Error C3867: "non-standard syntax; use '&' to create a pointer to member"
Question:
When attempting to develop a Tic Tac Toe game in C , a user encountered the error C3867: "non-standard syntax; use '&' to create a pointer to remember". Despite attempting suggested solutions, the error persists. How can it be resolved?
Answer:
The error message indicates that the code is attempting to use a member function name without using the address-of operator (&) to create a pointer to the member function.
In non-member functions, the function name can be used directly in an expression without using the function call syntax. However, in member functions, using the member function name without the call syntax is invalid.
To obtain a pointer to a member function, the & operator must be used:
<code class="c++">struct Bar { void baz() {} }; &Bar::baz; // Valid</code>
Applying this concept to the provided code, the issue occurs in the player1Move function:
<code class="c++">void TicTacToe::player1Move(string coordX) // ERROR { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; }</code>
In this code, the player1Move function is a member function of the TicTacToe class, and the error is caused by using the function name player1Move without the address-of operator:
<code class="c++">Board.player1Move; // ERROR: Should be Board.player1Move() or &Board.player1Move</code>
By adding the & operator, the code will correctly create a pointer to the member function:
<code class="c++">void TicTacToe::player1Move(string coordX) { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; } Board.&player1Move; // Valid pointer to member function</code>
The above is the detailed content of How to Resolve \"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!