C ++ 11中的Numerical Recipes v3.0和pthread库

时间:2013-10-20 12:49:18

标签: c++ c++11 numerical numerical-computing

如果我使用g++ -std=c++11 Threads.cpp -lpthread -I../Libs/nr30命令编译以下代码,其中nr30是http://www.nr.com/提供的库,我没有得到任何错误:

#include <chrono>
#include <thread>

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

如果我在上面的代码中添加#include "nr3.h"标头,我会收到以下错误:

 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

你遇到过同样的问题吗?谢谢你的任何建议。

1 个答案:

答案 0 :(得分:2)

看起来这个库的作者正在定义他们自己的异常类,当用C ++ 11编译代码时,他们忘记将what()方法标记为noexcept(或者他们正在派生来自弃用的异常类,例如TR1)。这再现了问题:

#include <stdexcept>

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

int main()
{
}

并修复了它:

#include <stdexcept>

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

int main()
{
}

这意味着您必须自己修补库(如果您可以访问完整的源代码),或者向他们发送错误报告。

(你仍然应该考虑使用-pthread代替-lpthread,它更干净,避免进一步的错误)