gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:06:46 24 4
gpt4 key购买 nike

如果我使用命令 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" header ,则会出现以下错误:

 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

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

最佳答案

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

关于c++ - C++11 中的 Numerical Recipes v3.0 和 pthread 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com