Home  >  Article  >  Backend Development  >  How to Resolve \"non-standard syntax; use \'&\' to create a pointer to member\" Error in Visual Studio 2015?

How to Resolve \"non-standard syntax; use \'&\' to create a pointer to member\" Error in Visual Studio 2015?

DDD
DDDOriginal
2024-10-31 20:26:02921browse

How to Resolve

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() {}
};

&amp;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 &amp;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.&amp;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn