0

如果我使用命令编译以下代码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

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

4

1 回答 1

2

看起来这个库的作者正在定义他们自己的异常类,并且他们忘记将what()方法标记为noexcept使用 C++11 编译代码时的方法(或者它们是从已弃用的异常类派生的,例如 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,它更清洁并避免进一步的错误)

于 2013-10-20T14:13:20.397 回答