我學校的編譯器似乎不支持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’
與此問題無關,但請避免手動鎖定和解鎖互斥鎖。遵循RAII範例,改爲使用'std :: unique_lock'或'std :: lock_guard'。這樣,你就不會忘記解鎖你的互斥鎖,並且在異常情況下你可以防止鎖泄漏。 –
顯示的錯誤很簡單,只需將初始化移動到構造函數即可。但是你使用''庫,它根本就不存在,不能被「固定」 –
std :: string在任何標準中都沒有叫_Equal的成員,你可以使用==操作符。 – Eelke