DEV Community

CoderLegion
CoderLegion

Posted on • Edited on • Originally published at kodblems.com

C++ Error: Require a type specifier for all declarations

πŸŽ‰ Before you dive into this article...

πŸš€ Check out our vibrant new community at CoderLegion.com!

πŸ’‘ Share your knowledge, connect with like-minded developers, and grow together.

πŸ‘‰ Click here to join now!

Problem:
Hi there! I am a newbie and started learning to program recently. While doing programming, as a beginner I encountered many errors but the most rapidly occurring error was β€œC++ requires a type specifier for all declarations”.
Although I managed to resolve the error every time,
I want to know why this error is generated and how can I avoid it?

Solution:
This error occurs when you do not specify the datatype of a variable or you forget to specify the return type of a method or a function. Specifying the datatype of variables or the return-type of methods is necessary for C++ because it is a statically-typed language.

Statically-typed languages are one that requires each and everything to be specified by the user. Before using a variable or assigning it a value, you must tell the compiler what would be the type of data that is going to be stored in that variable. Similarly, while declaring functions, you must specify what type of data that function is going to return.
A statically-typed language also requires you to declare and define the functions above the main() function or some other calling function.

Consider the following examples:

Note:

Both examples generate different errors due to above-mentioned reason:

//This code does not generate the same error,
//but the error is caused due to the same reason

include

using namespace std;

int main()
{
a = 4; //data type not specified
}
.

//This program generates a different error due to the same reason.

include

using namespace std;

int main()
{
int c = add(); //error: 'add()' was not declared in this scope
cout<<c;
}

int add()
{
return 3+4;
}
I hope this will help you
Thanks

Top comments (0)