简体   繁体   中英

No match for operator * when using boost::variant

I define my own variant type like so:

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. 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. 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():

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM