【发布时间】:2018-08-23 11:12:35
【问题描述】:
我制作了一个带有多种类型的boost::variant 的向量,如下所示:
std::vector<boost::variant<type1,type2,type3,...and so on>>
然后,我创建了一个 typedef 将整个类型重命名为 vecTypesVar 和名为 typesVar 的变体
typedef boost::variant<type1,type2,type3,...and so on> typesVar
typedef std::vector<typesVar> vecTypesVar
现在我已经在该向量上创建了一个 for 循环,并使用选项 .which 我正在尝试为每个类型进行这样的实现。
vecTypesVar type = getTypesData();
for (auto i = 0; i < type.size(); ++i)
{
switch (type[i].which())
{
case 0:
break;
case 1:
break;
case 2:
break;
default: ;
}
}
所以,问题就从这里开始了,在 switch 的每个 case 上,我都试图使变量的类型与出现的变量相同。 p>
例如:
case 0:
auto data = boost::get<type1>(type[i]);
但我收到一条错误消息:
错误 C2248: 'type1::type1' : 无法访问 在“type1”类中声明的私有成员
我也尝试过像这样使用访问者: 模板
class port_visitator : public boost::static_visitor<T>
{
T operator()(typesVar type) const
{
return boost::get<T>(type);
}
};
并在我的开关中使用它:
case 0:
auto data = apply_visitor(port_visitator<type1>(), type[i]);
但我收到同样的错误:
错误 C2248: 'type1::type1' : 无法访问私有 在“type1”类中声明的成员
编辑: 这是 type1 的构造函数:
template<typename T, dataType ST>
class type1: public sometype<T, ST>, public othertype<T, ST>, public anothertype
{
public:
template <typename T1>
type1(
CSimSwcBase* _plgInstance,
const char* _name,
const T1& _initialValue,
const long _version,
const SimFunc_t _eventFunc = NULL,
void* _eventFuncInstance = NULL,
const long _size = GetSize<T>::SIZE,
const unsigned long _portMode = 0,
const bool _definedDefaultValue = false,
void* _pDefaultValue = NULL,
const unsigned long _defaultValueSize = 0,
const bool _bBuffered = false)
: sometype(_plgInstance, _name, _initialValue, _size, _portMode, _eventFunc, _eventFuncInstance, _definedDefaultValue, _pDefaultValue, _defaultValueSize, _bBuffered)
, othertype(dynamic_cast<sometype*>(this))
, m_bUseSyncRef (false)
, m_bGetDefaultValues(false)
, m_bReportError(true)
, m_errorCount(0)
, m_maxError(100)
, m_bDoOnlyStartupTest(false)
, m_bStartupTestPassed(false)
, m_bNotConnectedReported(false)
{
setupVersion(_version);
}
}
有没有人知道我该如何解决这个问题?
提前致谢:D
【问题讨论】:
标签: c++ visual-studio c++11 boost