2016-09-15 43 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可以在一些系统中,宏基,你应该小心大写的名字。