Home  >  Article  >  Backend Development  >  Why Am I Getting a \"Non-standard Syntax\" Error in My C Code?

Why Am I Getting a \"Non-standard Syntax\" Error in My C Code?

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 01:25:30663browse

Why Am I Getting a

Understanding the "non-standard syntax" Error

When writing code in C , programmers often encounter errors like "non-standard syntax." This error occurs when an operation is performed using syntax that is not officially recognized by the C language.

Issue: Missing Member Function Pointer

In your specific case, the error originates from the following code:

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

Here, the player1Move function is a member function of the TicTacToe class. However, the function name is used as a single expression, without invoking the function call syntax. According to C conventions, this is incorrect for member functions.

Resolution: Using the Address-of Operator

To correctly obtain a pointer to a member function, you need to use the address-of operator (&). This operator creates a reference to the function memory location, enabling us to access the function indirectly.

The fix to your code is:

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

With this change, the player1Move function can be invoked correctly by other parts of the program using the . operator, like this:

<code class="cpp">TicTacToe Board;
Board.player1Move("1");</code>

The above is the detailed content of Why Am I Getting a \"Non-standard Syntax\" Error in My C Code?. 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