Home > Backend Development > C++ > body text

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

DDD
Release: 2024-10-31 20:26:02
Original
919 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!