2016-09-15 46 views
0

我怎麼能初始化原子變量到結構,提高::原子MAX我怎麼能初始化的boost ::原子變量

我想:

#include <boost/atomic.hpp> 

struct mem { 
    // error: conversion from ‘int’ to non-scalar type ‘boost::atomics::atomic<int>’ requested 
    boost::atomic<int> MAX = 100; 

    // error: expected identifier before numeric constant 
    boost::atomic<int> MAX(100); 

    // error: ‘boost::atomics::atomic<T>::atomic(const boost::atomics::atomic<T>&) [with T = int]’ is private 
    boost::atomic<int> MAX = (boost::atomic<int>) 100; 

    // warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 
    boost::atomic<int> MAX{100}; 
} 

注:我可以不使用C + +11或C++ 14。

+0

你需要在構造函數中初始化它們 – Danh

回答

0

這種形式應該工作

boost::atomic<int> MAX(100); 

如果沒有,這可能意味着MAX令牌是由預處理器取代。儘量消除頭,使用不同的變量名等

參見

0

如果需要初始化C++ 03一個結構/類成員,那麼你必須寫一個構造函數。

struct mem { 
    boost::atomic<int> MAX; 

    mem() : MAX(100) 
    { 
    } 
}; 

PS:sehe是正確的,警告你MAX可以在一些系統中,宏基,你應該小心大寫的名字。