C++

C++ Error: Expected Primary Expression Before

While writing a code, a programmer may encounter some ambiguities. There are various types of errors like logical errors, compile-time errors, or syntax error. Logical errors are difficult to understand because it doesn’t show any error while syntax and compile time errors don’t even execute; they generate an error while the compilation of the code starts.

Today, we will discuss about a syntax error that may occur due to something missing in the code or something that is added to the code. The error that we are going to discuss is “expected primary expression”. This error mainly occurs when there is some missing keyword. To get to know this error, there is a keyword or character at the end which shows the reason for the error occurrence. When the syntax is not correctly followed by the coder, it does execute successfully. It displays the error which indicates the line where it is encountered.

The major reasons for the occurrence of the error are:

  • If the datatype of the argument is also specified with the variable, it invokes an error.
  • If the passed argument is the wrong type, like how passing a string to the function is a wrong type because we cannot pass a string to the function.
  • If the curly braces are missing or extra braces are added by mistake which generates the error.
  • It also occurs when the parenthesis in the if statement does not have any expression or result.

Syntax:

As we discussed in the introduction, this is an error and not a piece of code that must have any specified syntax. The following is the error sample of what it looks like.

error: expected primary expression before ‘element’

In the previous error, “element” denotes the keyword in which any expression is missing or added. An expression can be a character, arithmetic value, braces, or a relational expression.

Example 1:

Now, we perform an example in which we perform an addition of two variables in a user-defined function and then display the sum of both values. Let us include our “iostream” and “string” header files. Iostream is used to enable the compiler to use the inbuilt I/O operations that are provided by it where the string enables us to work with the collection of characters. Next, we declare a user-defined function named “myfunct()” to which we pass two arguments for the variables that are calculated. Inside this function, we declare a variable named addition which is responsible for holding the sum of “var1” and “var2 variables.

Now, heading to the main function where we define the integer value “result” to which we assign the myfunct() method by passing two integer values, “20” and “30”. After that, we execute the return value of the function that is stored in the “result” variable using the cout statement that is provided by the iostream library.

#include <iostream>
#include <string>
using namespace std;

int myfunct(int var1, int var2) {

     int addition;
     addition = var1 + var2;
     return addition;
}

int main(){

    int result = myfunct( 20, 30);
    std::cout << "addition of var1 and var2 is:"<< result << std::endl;
}

After the execution of the code, we encounter two errors on line 12 which indicates an issue with the “int” element which means that we cannot pass the datatype along with the variable name as an argument to the function. Arguments are always passed without the datatype to the function call. Now, we remove the error by eliminating the datatypes that we pass to it and then re-execute the code.

After re-execution, we successfully displayed the return value of the addition, as we can see in the following screenshot:

Example 2:

Now, we try another example in which we calculate the length of the word, calculate its permutation, and display it. After including the header files, we declare the three functions. The  getdata() function which gets a string from the user. Inside this method, we declare a string variable, “word”. Using the “cin” statement, we get the string from the user and return its value. The second function is the “length_count()” which calculates the length of the string using the method length() and returns its value. The third function is “myfunc()”  which checks if the string length is greater than “1”. Then, it calculates the permutation and returns it otherwise. It simply returns the length of the string.

At last, we move to the main function where we declare a string variable named “word” to which we assigned the function call of the getdata(). This means that the data that we have taken from the user is assigned to the “Word” variable and creating another variable which is “wordlength” to which we assign the function call of the length_count() where we pass the “word” variable along with its “string” datatype. At last, using the cout statement, we display the result of the permutation of the string that we have taken from the user and execute the code.

#include <iostream>
#include <string>
using namespace std;

string getdata();

int length_count(string word);
int myfunc(int wordLength);

string getdata()
{

  string word;

  cout << "Please enter a word: ";
  cin >> word;

  return word;
}

int length_count(string word)
{

  int wordLength;
  wordLength = word.length();
  return wordLength;
}

int myfunc(int wordLength)
{

  if (wordLength == 1)
  {
    return wordLength;
  }
  else
  {
    return wordLength * myfunc(wordLength - 1);
  }
}

int main()
{
  string word = getdata();
  int wordLength = length_count(string word);

  cout << word << " has " << myfunc(wordLength) << " permutations." << endl;

  return 0;
}

After the execution, we get this error which displays that we made a mistake at line 43. We pass the datatype to the function along with the variable. As we know, we cannot pass the datatype to the variable. Now, we try to resolve this error by removing the datatype from the function call and execute it again.

After executing the code again, we successfully displayed the message to have the value from the user as we enter the “js” string. After that, the permutation of the code is displayed as shown in the following:

Conclusion

In this article, we studied the type of error which may be encountered while writing the code. Sometimes, the coder may miss something or he/she may add an expression or declared a wrong syntax for the functions or statements by mistake. By using the detailed review and implementing the examples, we briefly described the error and the reason for which this error may be encountered. We also discussed the methodology to resolve these errors. We hope that this article is helpful for you to resolve these kinds of errors.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.