C++

C++ ERROR: Expected Identifier Before Numeric Constant

Today, we will be discussing the compile time error that mostly occurs while writing code and we will solve that error. Compile time error is also known as a syntax error. We are creating a program and it mostly occurs when missed the braces, semicolons, etc. While coding facing error is not a big deal for the experienced programmer, the newbie may find it a hard task to resolve these errors. Sometimes it takes a whole week to resolve them or to understand the reason for the error.

The error that we will discuss in this tutorial is “ERROR: expected identifier before numeric constant”. This error mostly occurs when we are trying to initialize or declare the data members outside the method without using braces. Before C++11 was introduced, a coder could first declare them and then we will initialize the value to them. Or we can say that outside the class, we cannot initialize the values using the constructor.

Let us check out the syntax. We will perform an example to learn how to remove these errors and the way to check the reason.

Syntax:

There is no such specified syntax for this error because it is not the command for performing some action or the function call. This error looks like as follows:

ERROR: expected identifier before numeric constant

In the error above, identifier indicates the unique names of the variables. The numeric constant is the collection of numerals, leading signs, or decimal points. For example, 3.0, 3, -2. The invalid numeric constants are 2-, 1b, 3.., 4. Etc.

Example # 01:

Let us perform an example to check how the error occurs and the way to resolve these errors. Before moving to our main code, we will include our header files that are “vector”, “string”, “sstream”, “fstream”, “isotream” and the “cmath”. The vector header file is the same as the dynamic arrays, they can resize themselves while the new data is entered into the array. As we are going to work with the arrays of the dynamic type, so we choose vector arrays, which provide various functions to work with arrays. The second-string header file we included is because we are going to work with a string of characters. And to work with it, it is necessary to include the string header file. After that, we included “fstream”, “sstream” and the “isostream”. Fstream is used to enable the coder to work with the file system. The sstream is included to work with stringstream which is used to associate the object to the stream allowing it to read the string if it is streamed in, where iostream is used to perform the I/O operations.

Finally, the “cmatch” header file is included which is used to enable the coder to perform various calculations using the inbuilt functions provided by the “cmath” library. Then, using the namespace std, we allowed the coder to define the variable that is provided by the std libraries. After including all the essential header files, we created a vector string using the syntax “vector <string> name()”. This is the specified syntax for creating a vector string. In our case, we created a vector string variable of name “var1” to which we passed two parameters: the first one is the number of the string and the second is the null value which means we do not want to store anything in it right now.

In the next step, we declared another string-type vector array named “var2” to which we passed two arguments: the first one is the number of the strings and the second one is the “var1”. After that, we declared a class named “my_class” inside which we declared another vector string named “var_name” to which we passed the number of strings “5”. After that another vector array of type integer we have declared which, we named “val”, in the “val” array we passed two parameters the number of strings “5” and the value 0 assigned to it.

Next, we have declared a function that is public for the whole code which will execute the message “error resolved”. Now, in our main function, we declared a variable name “obj” of type “my_class” to call the “myclass” variables and functions. At the end, we will return the null value and using the f7 command we will execute the code.

#include <vector>
#include <string>
#include <sstream>
#include <fstream>
# include <iostream>
#include <cmath>

using namespace std;

vector<string> var1(6, "null");
vector< vector<string> > var2(20,var1);

class my_class
{
     vector<string> var_name(5);
    vector<int> val(5,0);
    public:
    my_class(){
        cout << "error resolved" <<endl;
    }

};  

int main()
{  
my_class obj;
return 0;
}

Now, we will check out the output of the code that is displayed below the error. Two lines has been displayed, online “15” and “16”. This error shows “error: expected identifier before numeric constant”. This means that we are initializing the variable out of the scope and something is missing whether braces, commas, or else. To resolve this error, we will read both lines and check out for the reason for this error and will resolve it.

To resolve this error, we just replaced this code of vector string declaration with the one we have declared in the above code. The right method to initialize the vector variable is the one that is displayed below: by using the assignment operator we assign the values to the vector variable, or by passing it via curly braces.

After resolving the error, we executed our code again and executed code successfully with the message displayed in the snippet below.

Conclusion

In this tutorial, we have briefly studied an error that most frequently occurs at compile time, which is the syntax error. This type of error mostly occurs when the programmer has initialized the data outside the class. And they most probably occurred due to bad programming practices. We expect that this article will be advantageous to you while working with classes and constructors. You can try more examples to better understand how these types of errors would be resolved.

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.