Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did this C++ program fail in compilation?

I was reading this. I tested this program on code blocks 13.12 IDE which supports C++11 but it is getting failed in compilation & compiler shows multiple errors. Look at the program. It works fine on online compiler see this

// bad_array_new_length example
#include <iostream>     // std::cout
#include <exception>    // std::exception
#include <new>          // std::bad_array_new_length

int main() {
  try {
    int* p = new int[-1];
  } catch (std::bad_array_new_length& e) {
    std::cerr << "bad_array_new_length caught: " << e.what() << '\n';
  } catch (std::exception& e) {   // older compilers may throw other exceptions:
    std::cerr << "some other standard exception caught: " << e.what() << '\n';
  }
}

Compiler errors:

7   12      [Error] expected type-specifier

7   37      [Error] expected unqualified-id before '&' token

7   37      [Error] expected ')' before '&' token

7   37      [Error] expected '{' before '&' token

7   39      [Error] 'e' was not declared in this scope

7   40      [Error] expected ';' before ')' token

9   5       [Error] expected primary-expression before 'catch'

9   5       [Error] expected ';' before 'catch'

What is going wrong here? Is it a compiler bug or is C++11 not fully supported in code blocks 13.12 IDE?

Please help me.

like image 676
Destructor Avatar asked Apr 18 '15 12:04

Destructor


1 Answers

Your compiler does not support std::bad_array_new_length.

The top Google result for code blocks 13.12 says:

The codeblocks-13.12mingw-setup.exe file includes the GCC compiler and GDB debugger from TDM-GCC (version 4.7.1, 32 bit).

and GCC 4.7.1 was released in 2012. According to this mailing list post, even trunk GCC has only supported std::bad_array_new_length since 2013.

From bisecting the GCC reference manuals, we can determine that GCC 4.8.4 doesn't have it but GCC 4.9.2 does. The "online compiler" you linked to runs GCC 4.9.2.

Long story short, you're going to need a newer GCC.

"C++11 support" is a very broad term and you'll find that, until very recently, it essentially never meant complete C++11 support. For example, C++11 regexes weren't supported at all until GCC 4.9, either.

like image 101
Lightness Races in Orbit Avatar answered Oct 01 '22 02:10

Lightness Races in Orbit