How to throw a C++ exception

Simple: The Standard Library comes with a nice collection of built-in exception objects you can throw. Keep in mind that you should always throw by value and catch by reference: You can have multiple catch() statements after each try, so you can handle different exception types separately if you want. You can also re-throw exceptions: And to … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to and then include the necessary header file: and the value of pi can be accessed via: In my math.h (2014) it is defined as: but check your math.h for more. An extract from the “old” math.h (in 2009): However: on newer platforms (at least on my 64 bit … Read more

What exactly is the difference between “pass by reference” in C and in C++?

There are questions that already deal with the difference between passing by reference and passing by value. In essence, passing an argument by value to a function means that the function will have its own copy of the argument – its value is copied. Modifying that copy will not modify the original object. However, when passing by reference, … Read more

g++ ld: symbol(s) not found for architecture x86_64

I had a similar warning/error/failure when I was simply trying to make an executable from two different object files (main.o and add.o). I was using the command: gcc -o exec main.o add.o But my program is a C++ program. Using the g++ compiler solved my issue: g++ -o exec main.o add.o I was always under the impression … Read more

Why are #ifndef and #define used in C++ header files?

Those are called #include guards. Once the header is included, it checks if a unique value (in this case HEADERFILE_H) is defined. Then if it’s not defined, it defines it and continues to the rest of the page. When the code is included again, the first ifndef fails, resulting in a blank file. That prevents … Read more

C++ string to double conversion

You can convert char to int and viceversa easily because for the machine an int and a char are the same, 8 bits, the only difference comes when they have to be shown in screen, if the number is 65 and is saved as a char, then it will show ‘A’, if it’s saved as … Read more

C++ Cout & Cin & System “Ambiguous” [closed]

This kind of thing doesn’t just magically happen on its own; you changed something! In industry we use version control to make regular savepoints, so when something goes wrong we can trace back the specific changes we made that resulted in that problem. Since you haven’t done that here, we can only really guess. In … Read more

What is the effect of extern “C” in C++?

extern “C” makes a function-name in C++ have C linkage (compiler does not mangle the name) so that client C code can link to (use) your function using a C compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)