Another reason why this error occurs is that sometimes the programmer forgets to specify the datatype of variables and sometimes he does not simply know that specifying a datatype is mandatory. The reason why we have to specify the datatype of variables and why we have to specify the return type of our code is because C++ is a statically typed language. C++ is a statically typed language is that these types of languages need everything to be manually specified by the programmer. So, we must tell the compiler what datatype our variable will be off and this is in the case of declaring functions as well. These languages also require declared and defined functions above the main calling function by the coder.
Syntax
There is no specific syntax for this error. The following message is what our compiler will return if we get the above-mentioned error:
Note: The error message will not always be the same but it will explain the same thing about specifying a datatype or return type.
Example # 01:
Now, we will see what practice one does that leads to our error. We will also try to resolve the error and explain the reason in our code why this error occurred. To do so, we have declared a string variable “pword” and we have asked the user to input the value that will be assigned to it. After that, we applied a condition that if the value of our input parameter matches “TestPword”, the system will print “Correct” and will exit successfully. If the strings do not match, the system will print “Wrong” and will execute the code successfully in this case also. Now we will execute our code and will observe what happens.
using namespace std;
main()
{
string pword;
cin>>pword;
if (pword == "TestPword")
{
cout<< "Correct!" <<endl;
} else
{
cout<< "Wrong!" <<endl;
}
return 0;
}
Our code has not executed successfully and has returned an error. This error says that C++ Language does not allow the declaration of the main function without a datatype. It has also pointed out the line in which the compiler threw an error. So, our error demands that we need to specify the datatype of our main method.
After the error, we will type “int” before the main method to let the system know that the main function has an integer datatype and return type. Now, we will execute our code.
After the above changes, we executed our code successfully. The system has asked the user to insert a string type parameter. The string is compared to the value we hard coded in our If condition. We provided “123456789” as our input parameter and the value to be compared with was “TestPword”. In our case, the values do not match so the system has printed “Wrong”. So, by only specifying the datatype and return type of our function we have resolved the error.
Example # 02:
In the previous example, we got the error because we did not declare the datatype of our main function. Let us perform this example and see whether we get any error and if we do so then we will try to find out the problem behind it and eliminate it. A variable named “x” has been declared with the value “10.” We have also assigned another variable “y” and passed it “20”.”z” is the variable that will store the result of the operation between the two variables. After the operation, we will print out “z” to check the result.
using namespace std;
int main()
{
x = 10;
y = 20;
z = x+ y; cout<<z<<endl;
}
This is the error we will get after the execution of our code. It has three errors. All three are the same: error: ‘x’ was not declared in this scope. And the same error is for other variables as well. This error has occurred because we have not specified the datatypes of our variables. As discussed earlier, this is the same error but the message is different.
Now, we will correct our code and to do so we have simply assigned datatypes “int” to all three variables. Now, the system will understand that all three are integers and will perform the execution accordingly.
Our program has successfully executed and returned 30 as the output. In our code, the operation we performed between the “x” and “y” parameters is the sum or add operation. We passed values 10 to “x” and 20 to “y”. So, the variable z has stored the sum of these two values which is 30 and printed it as the output. So, by simply assigning datatypes to our parameters, we have simply resolved the error in our code.
Conclusion
In this guide, we discussed an error which is C++ required a type specifier for all declarations. We explained what this error is and why this error occurs while writing our code. We also performed different examples to get the error and then after getting the error messages we resolved those errors and told you the ways to resolve them. We also discussed different scenarios of the error and explained that the message of the error might be a little different, but the cause of the error remains the same in all cases.