Home > Backend Development > C++ > body text

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

Barbara Streisand
Release: 2024-11-02 01:25:30
Original
662 people have browsed it

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

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

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

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!

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!