[英]No match for operator * when using boost::variant
I define my own variant
type like so: 我定义自己的
variant
类型,如下所示:
typedef variant<myobj **, ... other types> VariantData;
One of my class methods gets this data type as a parameter and tries to do something like: 我的一种类方法将这种数据类型作为参数,并尝试执行以下操作:
void MyMethod(VariantData var){
//method body
if(some_cond){ // if true, then it implies that var is of type
// myobj **
do_something(*var); // however, I'm unable to dereference it
}
// ... ther unnecessary stuff
}
As a result, when I compile my program, I get this error message: 结果,当我编译程序时,出现以下错误消息:
error: no match for 'operator*' (operand type is 'VariantData ....'
I do not know how to fix this error. 我不知道如何解决此错误。 PS.
PS。 On the whole the code works well - if I comment out this part related to dereferencing, then everything runs smoothly.
总体而言,该代码运行良好-如果我注释掉与解引用有关的这一部分,那么一切都会顺利进行。
The error message is quite self-explanatory: you can't dereference boost::variant
, it doesn't have such semantics. 错误消息是不言自明的:您不能取消引用
boost::variant
,它没有这种语义。 You should first extract the value, ie the pointer, and then dereference it. 您应该首先提取值(即指针),然后取消引用。
To extract the value relying on a run-time logic, just call get(): 要提取依赖于运行时逻辑的值,只需调用get():
//method body
if(some_cond){ // if true, then it implies that var is of type myobj **
do_something(*get<myobj **>(var));
}
Note, however, that if the run-time logic fails (eg. due to a bug), get()
will throw bad_get
exception. 但是请注意,如果运行时逻辑失败(例如,由于错误),则
get()
将引发bad_get
异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.