-->

Beginner C++ Mistakes

There are many pitfalls the C++ developer can encounter, especially as this is a more complex and often unforgiving language to master. Beginners need to take C++ a step at a time and digest what they’ve learned before moving on.


VOID(C++, MISTAKES)


Admittedly it’s not just C++ beginners that make the kinds of errors we outline on these pages, even hardened coders are prone to the odd mishap here and there. Here are some common issues to try and avoid.

UNDECLARED IDENTIFIERS


A common C++ mistake, and to be honest a common mistake with most programming languages, is when you try and output a variable that doesn’t exist. Displaying the value of x on-screen is fine but not if you haven’t told the compiler what the value of x is to begin with.



SEMICOLONS


Remember that each line of a C++ program must end with a semicolon. If it doesn’t then the compiler treats the line with the missing semicolon as the same line with the next semicolon on. This creates all manner of problems when trying to compile, so don’t forget those semicolons.



STD NAMESPACE


Referencing the Standard Library is common for beginners throughout their code, but if you miss the std:: element of a statement, your code errors out when compiling. You can combat this by adding:

using namespace std;

Under the #include part and simply using cout, cin and so on from then on.



GCC OR G++


If you’re compiling in Linux then you will no doubt come across gcc and g++. In short, gcc is the Gnu Compiler Collection (or Gnu C Compiler as it used to be called) and g++ is the Gnu ++ (the C++ version) of the compiler. If you’re compiling C++ then you need to use g++, as the incorrect compiler drivers will be used.



COMMENTS (AGAIN)


Indeed the mistake of never making any comments on code is back once more. As we’ve previously bemoaned, the lack of readable identifiers throughout the code makes it very difficult to look back at how it worked, for both you and someone else. Use more comments.



QUOTES


Missing quotes is a common mistake to make, for every level of user. Remember that quotes need to encase strings and anything that’s going to be outputted to the screen or into a file, for example. Most compilers errors are due to missing quotes in the code.



EXTRA SEMICOLONS


While it’s necessary to have a semicolon at the end of every C++ line, there are some exceptions to the rule. Semicolons need to be at the end of every complete statement but some lines of code aren’t complete statements. Such as:

#include
if lines
switch lines

If it sounds confusing don’t worry, the compiler lets you know where you went wrong.



TOO MANY BRACES


The braces, or curly brackets, are beginning and ending markers around blocks of code. So for every { you must have a }. Often it’s easy to include or miss out one or the other facing brace when writing code; usually when writing in a text editor, as an IDE adds them for you.



INITIALISE VARIABLES


In C++ variables aren’t initialised to zero by default. This means if you create a variable called x then, potentially, it is given a random number from 0 to 18,446,744,073,709,551,616, which can be difficult to include in an equation. When creating a variable, give it the value of zero to begin with: x=0.



A.OUT


A common mistake when compiling in Linux is forgetting to name your C++ code post compiling. When you compile from the Terminal, you enter:

g++ code.cpp

This compiles the code in the file code.cpp and create an a.out file that can be executed with ./a.out. However, if you already have code in a.out then it’s overwritten. Use:

g++ code.cpp -o nameofprogram




0 Response to "Beginner C++ Mistakes"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel