2014-02-18 30 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来保护对您的媒介的访问。