0

If I compile the following code with the command g++ -std=c++11 Threads.cpp -lpthread -I../Libs/nr30, where nr30 is the library provided by http://www.nr.com/, I obtain no errors:

#include <chrono>
#include <thread>

int main(void) { 
  /* ... Sadly, No further code in here was added... */
}   

while if I add the #include "nr3.h" header in the same code as above, I obtain the following error:

 In file included from /usr/include/c++/4.7/memory:76:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/ext/concurrence.h:73:5: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/ext/concurrence.h:72:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_lock_error::what() const’
 In file included from /usr/include/c++/4.7/ios:40:0,
                  from /usr/include/c++/4.7/istream:40,
                  from /usr/include/c++/4.7/fstream:40,
                  from ../Libs/nr30/nr3.h:10,
                  from random_test.cpp:29:
 /usr/include/c++/4.7/exception:70:25: error:   overriding ‘virtual const char* std::exception::what() const noexcept (true)’
 In file included from /usr/include/c++/4.7/memory:76:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_lock_error::what() const’:
 /usr/include/c++/4.7/ext/concurrence.h:72:18: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/ext/concurrence.h: At global scope:
 /usr/include/c++/4.7/ext/concurrence.h:81:5: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/ext/concurrence.h:80:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_unlock_error::what() const’
 In file included from /usr/include/c++/4.7/ios:40:0,
                  from /usr/include/c++/4.7/istream:40,
                  from /usr/include/c++/4.7/fstream:40,
                  from ../Libs/nr30/nr3.h:10,
                  from random_test.cpp:29:
 /usr/include/c++/4.7/exception:70:25: error:   overriding ‘virtual const char* std::exception::what() const noexcept (true)’
 In file included from /usr/include/c++/4.7/memory:76:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_unlock_error::what() const’:
 /usr/include/c++/4.7/ext/concurrence.h:80:18: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/ext/concurrence.h: At global scope:
 /usr/include/c++/4.7/ext/concurrence.h:89:5: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/ext/concurrence.h:88:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_broadcast_error::what() const’
 In file included from /usr/include/c++/4.7/ios:40:0,
                  from /usr/include/c++/4.7/istream:40,
                  from /usr/include/c++/4.7/fstream:40,
                  from ../Libs/nr30/nr3.h:10,
                  from random_test.cpp:29:
 /usr/include/c++/4.7/exception:70:25: error:   overriding ‘virtual const char* std::exception::what() const noexcept (true)’
 In file included from /usr/include/c++/4.7/memory:76:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_broadcast_error::what() const’:
 /usr/include/c++/4.7/ext/concurrence.h:88:18: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/ext/concurrence.h: At global scope:
 /usr/include/c++/4.7/ext/concurrence.h:97:5: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/ext/concurrence.h:96:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_wait_error::what() const’
 In file included from /usr/include/c++/4.7/ios:40:0,
                  from /usr/include/c++/4.7/istream:40,
                  from /usr/include/c++/4.7/fstream:40,
                  from ../Libs/nr30/nr3.h:10,
                  from random_test.cpp:29:
 /usr/include/c++/4.7/exception:70:25: error:   overriding ‘virtual const char* std::exception::what() const noexcept (true)’
 In file included from /usr/include/c++/4.7/memory:76:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_wait_error::what() const’:
 /usr/include/c++/4.7/ext/concurrence.h:96:18: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/ext/concurrence.h: At global scope:
 /usr/include/c++/4.7/ext/concurrence.h:309:5: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/ext/concurrence.h: In destructor ‘__gnu_cxx::__scoped_lock::~__scoped_lock()’:
 /usr/include/c++/4.7/ext/concurrence.h:308:22: error: expected primary-expression before ‘,’ token
 In file included from /usr/include/c++/4.7/memory:84:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/backward/auto_ptr.h: At global scope:
 /usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
 /usr/include/c++/4.7/backward/auto_ptr.h:103:49: error: from this location
 /usr/include/c++/4.7/backward/auto_ptr.h:103:56: error: ‘__p’ was not declared in this scope
 /usr/include/c++/4.7/backward/auto_ptr.h:103:59: error: expected ‘;’ at end of member declaration
 /usr/include/c++/4.7/backward/auto_ptr.h:103:61: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
 /usr/include/c++/4.7/backward/auto_ptr.h:112:41: error: from this location
 /usr/include/c++/4.7/backward/auto_ptr.h:112:48: error: ‘__a’ was not declared in this scope
 /usr/include/c++/4.7/backward/auto_ptr.h:112:61: error: expected ‘;’ at end of member declaration
 /usr/include/c++/4.7/backward/auto_ptr.h:112:63: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
 /usr/include/c++/4.7/backward/auto_ptr.h:125:49: error: from this location
 /usr/include/c++/4.7/backward/auto_ptr.h:125:56: error: ‘__a’ was not declared in this scope
 /usr/include/c++/4.7/backward/auto_ptr.h:125:69: error: expected ‘;’ at end of member declaration
 /usr/include/c++/4.7/backward/auto_ptr.h:125:71: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:137:7: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:155:9: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:182:7: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:195:7: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:211:27: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:226:7: error: expected unqualified-id before ‘{’ token
 /usr/include/c++/4.7/backward/auto_ptr.h:241:7: error: expected unqualified-id before ‘{’ token
 random_test.cpp:126:1: error: expected ‘}’ at end of input
 In file included from /usr/include/c++/4.7/memory:84:0,
                  from /usr/include/c++/4.7/thread:40,
                  from random_test.cpp:34:
 /usr/include/c++/4.7/backward/auto_ptr.h: In constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>::element_type*)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:103:39: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In copy constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:112:31: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:125:39: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>& std::auto_ptr<_Tp>::operator=(std::auto_ptr<_Tp>&)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:136:32: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>& std::auto_ptr<_Tp>::operator=(std::auto_ptr<_Tp1>&)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:154:40: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type& std::auto_ptr<_Tp>::operator*() const’:
 /usr/include/c++/4.7/backward/auto_ptr.h:181:25: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::operator->() const’:
 /usr/include/c++/4.7/backward/auto_ptr.h:194:26: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::get() const’:
 /usr/include/c++/4.7/backward/auto_ptr.h:211:19: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::release()’:
 /usr/include/c++/4.7/backward/auto_ptr.h:225:17: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘void std::auto_ptr<_Tp>::reset(std::auto_ptr<_Tp>::element_type*)’:
 /usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected primary-expression before ‘,’ token
 /usr/include/c++/4.7/backward/auto_ptr.h: At global scope:
 /usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected unqualified-id at end of input
 /usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected ‘}’ at end of input
 make: *** [rtest] Error 1

Did you encounter the same problem? Thank you for any advice.

jackb
  • 695
  • 8
  • 26
  • Before downgrading the lot, the Numerical Recipes library was the one provided by http://www.nr.com/ – jackb Oct 20 '13 at 12:54
  • @DeletedReply Sorry, that seems to be a header-inclusion problem: not even in by linking against `-pthread` the problem was solved... :| – jackb Oct 20 '13 at 14:01

1 Answers1

2

It looks like the authors of this library are defining their own exception classes and they forgot to mark the what() method as noexcept when the code is compiled with C++11 (or they are deriving from deprecated exception classes, e.g. TR1). This reproduces the problem:

#include <stdexcept>

struct my_exception : std::exception
{
    const char* what() const { return "what"; } 
};

int main()
{
}

and this fixes it:

#include <stdexcept>

struct my_exception : std::exception
{
    const char* what() const noexcept { return "what"; } 
    //                       ^^^^^^^^
};

int main()
{
}

This means you either have to patch the library yourself (if you have access to the full source code) or you send them a bug report.

(you should still consider using -pthread instead of -lpthread, it's much cleaner and avoids further bugs)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Well, The following code caused the error: `#ifndef _USENRERRORCLASS_ #define throw(message) \ {printf("ERROR: %s\n in file %s at line %d\n", message,__FILE__,__LINE__); throw(1);} #else struct NRerror { char *message; char *file; int line; NRerror(char *m, char *f, int l) : message(m), file(f), line(l) {} }; #define throw(message) throw(NRerror(message,__FILE__,__LINE__)); void NRcatch(NRerror err) { printf("ERROR: %s\n in file %s at line %d\n",err.message, err.file, err.line); exit(1); }` #endif – jackb Oct 20 '13 at 14:36