2017-10-13 90 views
1

我學校的編譯器似乎不支持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’ 
+1

與此問題無關,但請避免手動鎖定和解鎖互斥鎖。遵循RAII範例,改爲使用'std :: unique_lock'或'std :: lock_guard'。這樣,你就不會忘記解鎖你的互斥鎖,並且在異常情況下你可以防止鎖泄漏。 –

+0

顯示的錯誤很簡單,只需將初始化移動到構造函數即可。但是你使用''庫,它根本就不存在,不能被「固定」 –

+0

std :: string在任何標準中都沒有叫_Equal的成員,你可以使用==操作符。 – Eelke

回答

1

,你可以通過初始化成員更換

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功能實例。

+1

'std :: mutex'是一個C++ 11功能。 –

+0

@FrançoisAndrieux在日誌中沒有看到這個錯誤,但你是對的。我記得在我編寫C++ 03之前使用'pthread'庫。 –

0

奇怪的是,您的編譯器不會抱怨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這將只要工作。如果情況並非如此,則必須單獨找到解決該問題的解決方案。