如何解决不支持的C++ 11代码
我学校的编译器似乎不支持C++ 11,但我不确定如何解决此问题。我也不确定是什么导致了最后一次编译器错误。我试图在Linux上编译:如何解决不支持的C++ 11代码
gcc -std=c++0x project2.cpp
系统无法识别-std = C++ 11。任何想法如何让它编译?谢谢!
#include <thread>
#include <cinttypes>
#include <mutex>
#include <iostream>
#include <fstream>
#include <string>
#include "time_functions.h"
using namespace std;
#define RING_BUFFER_SIZE 10
class lockled_ring_buffer_spsc
{
private:
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
void push(string val)
{
lock.lock();
buffer[write%size] = val;
write++;
lock.unlock();
}
string pop()
{
lock.lock();
string ret = buffer[read%size];
read++;
lock.unlock();
return ret;
}
};
int main(int argc, char** argv)
{
lockled_ring_buffer_spsc queue;
std::thread write_thread([&]() {
start_timing();
string line;
ifstream myfile("p2-in.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
line += "\n";
queue.push(line);
cout << line << " in \n";
}
queue.push("EOF");
myfile.close();
stop_timing();
}
}
);
std::thread read_thread([&]() {
ofstream myfile;
myfile.open("p2-out.txt", std::ofstream::out | std::ofstream::trunc);
myfile.clear();
string tmp;
while (1)
{
if (myfile.is_open())
{
tmp=queue.pop();
cout << tmp << "out \n";
if (tmp._Equal("EOF"))
break;
myfile << tmp;
}
else cout << "Unable to open file";
}
stop_timing();
myfile.close();
}
);
write_thread.join();
read_thread.join();
cout << "Wall clock diffs:" << get_wall_clock_diff() << "\n";
cout << "CPU time diffs:" << get_CPU_time_diff() << "\n";
system("pause");
return 0;
}
编译器错误:
project2.cpp:14:14: sorry, unimplemented: non-static data member initializers
project2.cpp:14:14: error: ISO C++ forbids in-class initialization of non-const static member ‘write’
project2.cpp:15:13: sorry, unimplemented: non-static data member initializers
project2.cpp:15:13: error: ISO C++ forbids in-class initialization of non-const static member ‘read’
project2.cpp:16:13: sorry, unimplemented: non-static data member initializers
project2.cpp:16:13: error: ISO C++ forbids in-class initialization of non-const static member ‘size’
project2.cpp: In lambda function:
project2.cpp:79:13: error: ‘std::string’ has no member named ‘_Equal’
,你可以通过初始化成员更换
class lockled_ring_buffer_spsc
{
private:
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
(除了静态数组)+默认的构造函数
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
//std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0),read(0),size(RING_BUFFER_SIZE)
{}
和
if (tmp._Equal("EOF"))
简直是
if (tmp == "EOF")
正如有人指出的那样,只有std::mutex
在C++ 11引入了您必须使用普通的C pthread_mutex_lock
/pthread_mutex_unlock
功能实例。
'std :: mutex'是一个C++ 11功能。 –
@FrançoisAndrieux在日志中没有看到这个错误,但你是对的。我记得在我编写C++ 03之前使用'pthread'库。 –
奇怪的是,您的编译器不会抱怨std::mutex
,这是一个C++ 11功能,但抱怨其他C++ 11功能。
线条
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
在C++ 11级但不是以前的版本有效。你可以在构造函数中初始化它们。
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0), read(0), size(RING_BUFFER_SIZE) {}
...
};
,因为编译器在预C++ 11版本支持std::mutex
这将只要工作。如果情况并非如此,则必须单独找到解决该问题的解决方案。
与此问题无关,但请避免手动锁定和解锁互斥锁。遵循RAII范例,改为使用'std :: unique_lock'或'std :: lock_guard'。这样,你就不会忘记解锁你的互斥锁,并且在异常情况下你可以防止锁泄漏。 –
显示的错误很简单,只需将初始化移动到构造函数即可。但是你使用''库,它根本就不存在,不能被“固定” –
std :: string在任何标准中都没有叫_Equal的成员,你可以使用==操作符。 – Eelke