Home > Backend Development > C++ > body text

Why Do I Get the \'non-standard syntax; use \'&\' to create a pointer to member\' Error in Visual Studio 2015?

Patricia Arquette
Release: 2024-10-31 20:53:02
Original
773 people have browsed it

Why Do I Get the

Visual Studio 2015: Understanding "non-standard syntax; use '&' to create a pointer to member" Error

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.

Explanation of the Error

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

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

Now consider a member function within a struct:

<code class="cpp">struct Bar
{
    void baz()
    {
        // Function body
    }
};</code>
Copy after login

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">&amp;Bar::baz; // Valid</code>
Copy after login

Resolving the Error in Your 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>
Copy after login
<code class="cpp">// Corrected version of player2Move
void TicTacToe::player2Move(string coordX)
{
    cout << "Enter X: " << endl;
    cin >> coordX;
    _coordX = coordX;
}</code>
Copy after login

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!

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
Latest Articles by Author
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!