我使用 macports 编译并安装了 gcc4.4。
当我尝试使用 -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...进行编译时...:
#include <thread>
...
std::thread t(handle);
t.join();
....
编译器返回:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
但是std::cout <<...
编译正常..
谁能帮帮我?
最佳答案
gcc 还不完全支持 std::thread:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
使用 boost::thread同时。
编辑
尽管使用 gcc 4.4.3 对我来说以下编译并运行良好:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
编译
g++ -Wall -g -std=c++0x -pthread main.cpp
a.out
的输出:
Printing from another thread
你能提供完整的代码吗?也许在那些 ...
中潜伏着一些不为人知的问题?
关于c++ - std::thread 错误(线程不是 std 的成员),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519607/