DEV Community

Cover image for Bug of the week #2
pikoTutorial
pikoTutorial

Posted on • Originally published at pikotutorial.com

Bug of the week #2

Welcome to the next pikoTutorial !

The error we're handling today is a C++ compilation error:

ISO C++ forbids declaration with no type
Enter fullscreen mode Exit fullscreen mode

What does it mean?

In C++, variables must be declared with a type to inform the compiler about the kind of data the variable will hold. A declaration without a type leaves the compiler clueless, resulting in a syntax error. Consider the following code snippet:

variable = 10;
Enter fullscreen mode Exit fullscreen mode

This line triggers the error because variable is not declared with a type. The compiler doesn't know whether variable is an integer, a float or any other data type.

How to fix it?

Simply add a type in your variable declaration:

int variable = 10;
Enter fullscreen mode Exit fullscreen mode

You can omit type when using auto keyword:

auto variable = 10;
Enter fullscreen mode Exit fullscreen mode

Now the compiler will deduce the type of the variable you've just initialized (in this case, 10 will be interpreted as an int). Remember, however, that to make type deduction possible, the variable declaration must be combined with a variable initialization. The following usage of auto is invalid and will trigger a compiler error anyway because there is no value for the compiler to determine the type of variable:

auto variable;
Enter fullscreen mode Exit fullscreen mode

Note for beginners: be careful when using auto for complex types because some results may be surprising. For example, by saying auto v = {1, 2, 3} you may want to create a vector, but this is actually as std::initializer_list.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay