2014-02-18 32 views
0

訪問下面的代碼崩潰我的程序:揮發性成員不能由成員函數

#include <string> 
#include <vector> 

class test { 
volatile std::vector<std::string> wtf; 
public: 
    test() {} 
    void dope() { wtf.clear(); } 
}; 

int main(){ 
    (new test())->dope(); 
    return 0; 
} 

而且我不知道爲什麼。當我刪除易失性時,它再次工作。那麼,爲什麼波動很大?

+0

我認爲真正的問題是你爲什麼要'volatile'std :: vector? – jrok

+0

我得到了(用gcc 4.8.1):'error:passing'volatile std :: vector >'as'this'argument'void std :: vector <_Tp, _Alloc> :: clear()[with _Tp = std :: basic_string ; _Alloc = std :: allocator >]'丟棄限定符[-fpermissive]' – Jarod42

+0

它包含必須由程序每次訪問的信息。它是多線程的。 – user2741831

回答

2

std::vector::clear()沒有volatile限定符。

因此用易失性向量來調用它是非法的。

順便說一下,volatile是不是多線程的魔術關鍵字。
您可以使用mutex來保護對您的媒介的訪問。