Logo

dev-resources.site

for different kinds of informations.

Bug of the week #2

Published at
1/7/2025
Categories
cpp
compiling
pikotutorial
Author
pikotutorial
Categories
3 categories in total
cpp
open
compiling
open
pikotutorial
open
Author
12 person written this
pikotutorial
open
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.

cpp Article's
30 articles in total
Favicon
C++ 1st code
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
As 10 Linguagens de Programação mais velozes do mundo
Favicon
The 10 fastest programming languages in the world
Favicon
Concurrency in C++: Mitigating Risks
Favicon
C++ or Rust? I'd stick to my good old C++
Favicon
Day 7: Unlocking the Power of Loops in C++
Favicon
Application scenarios of QML and C++ hybrid programming
Favicon
Bag: A header-only, simplistic, C++20, zipping & unzipping library.
Favicon
ИСТОРИЯ .NET
Favicon
How to Build a Node.js Camera Addon and Using It for Image Processing
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
C++20 - s(uper)size
Favicon
5 ways of passing unique pointer to a function in C++
Favicon
Securing C++ iostream: Key Vulnerabilities and Mitigation Strategies
Favicon
Reading Text Files Like a Pro - C++
Favicon
Day 7 : C++ language | Comparison Operators
Favicon
Day 5: C++ language | Arithmetic Operators
Favicon
Day 6: C++ Language | Assignment operators
Favicon
Day 6: Learning Conditionals – The Building Blocks of Logic
Favicon
OpenMP Data-Sharing Clauses: Differences Explained
Favicon
How to correctly rotate an OBB ?
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Day 3: C++ language | Variables | Datatypes | Part-1
Favicon
Bug of the week #2
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Antivirus in C++
Favicon
Let’s review some code #1: C++
Favicon
Switch Case
Favicon
Recording of selected portion of screen in .MP4 format using C++ in RAD studio

Featured ones: