2017-10-13 14 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; 
} 

コンパイルエラー:あなたは(静的な配列を除く)初期化されていないメンバーによって

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: 

を置き換えることができ

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; 
    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")) 

誰かが指摘したようにstd::mutexだけあなたは、例えばpthread_mutex_lock/pthread_mutex_unlock機能をプレーンなCを使用する必要がありますC++ 11で導入された、単に

if (tmp == "EOF") 

です。

+1

'std :: mutex'はC++ 11の機能です。 –

+0

@FrançoisAndrieuxはログにそのエラーを表示しませんでしたが、あなたは正しいです。私は '' pthread'ライブラリを使用していました。 –

0

コンパイラがC++ 11の機能であるstd::mutexについて文句を言っていないのは不思議ですが、他のC++ 11の機能については不平を言っています。

int write = 0; 
int read = 0; 
int size = RING_BUFFER_SIZE; 

ラインはC++ 11のではなく以前のバージョンに有効です。コンストラクタで初期化することができます。

これは、コンパイラがをpre-C++ 11バージョンでサポートしている限り動作します。そうでない場合は、その問題を個別に解決する必要があります。

関連する問題