Home > Backend Development > C++ > body text

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

Susan Sarandon
Release: 2024-11-01 17:58:30
Original
684 people have browsed it

Why Am I Getting the

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

Problem:

An error message is encountered during compilation in Visual Studio 2015, stating "non-standard syntax; use '&' to create a pointer to member." This error pertains to the attempt to call a member function without using the proper pointer syntax.

Cause:

In C , member functions are accessed using pointers to member functions rather than directly. When using member functions within non-member functions, it's essential to prepend the member function name with an '&' to indicate the pointer.

Solution:

To resolve this error, modify the code to use the proper pointer syntax when calling member functions. Here's how you can fix the issue in your "player1Move" member function:

<code class="cpp">void TicTacToe::player1Move(string coordX) // ERROR
{
  cout << "Enter X: " << endl;
  cin >> coordX;
  _coordX = coordX;
}</code>
Copy after login

The above code needs to be replaced with:

<code class="cpp">void TicTacToe::player1Move(string coordX)
{
  cout << "Enter X: " << endl;
  cin >> coordX;
  _coordX = coordX;
}</code>
Copy after login

By adding '&' before the "player1Move" function name, you create a pointer to the member function, allowing you to call it correctly.

Additional Explanation:

When a member function is not called explicitly on an object instance (as a method in object-oriented programming), it can be called using a pointer to a member function. The '&' operator in C is used to create a pointer to a member function. This syntax adheres to the C standard and allows for proper access to member functions from non-member contexts.

The above is the detailed content of Why Am I Getting 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!