When working with member functions in C , it's crucial to use the correct syntax to access them. The error "non-standard syntax; use '&' to create a pointer to member" arises when attempting to use a member function name directly as a function pointer.
In C , a non-member function can be used as a function pointer without any additional syntax. However, when dealing with member functions, using the member function name directly without the function call syntax is invalid. Instead, you need to use the & operator to obtain a pointer to the member function.
For example, consider the following non-member function:
<code class="cpp">void foo() { // Function body }</code>
In this case, you can directly use the foo function name in an expression like this:
<code class="cpp">foo; // Evaluates to a function pointer</code>
Now consider a member function within a struct:
<code class="cpp">struct Bar { void baz() { // Function body } };</code>
Attempting to use the baz member function name directly in an expression without the function call syntax will result in a compiler error. Instead, you need to use the & operator to obtain a pointer to the member function:
<code class="cpp">&Bar::baz; // Valid</code>
In your TicTacToe.cpp file, you define two member functions, player1Move and player2Move, which raise the error you're facing. The correct way to define these functions is to use the & operator as follows:
<code class="cpp">// Corrected version of player1Move void TicTacToe::player1Move(string coordX) { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; }</code>
<code class="cpp">// Corrected version of player2Move void TicTacToe::player2Move(string coordX) { cout << "Enter X: " << endl; cin >> coordX; _coordX = coordX; }</code>
By making these changes, you'll eliminate the "non-standard syntax" error and allow your program to access the member functions correctly.
The above is the detailed content of Why Do I Get 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!