0

I am implementing the following function:

bool DenStream::_try_merge(const double sample,
                           const double weight,
                           const std::optional<MicroCluster>& micro_cluster) const {
    if (micro_cluster) {
        MicroCluster micro_cluster_copy(*micro_cluster);
        micro_cluster_copy.insert_sample(sample, weight);
        if (micro_cluster_copy.get_radius() <= this->eps) {
            micro_cluster.insert_sample(sample, weight); // THIS DOES NOT WORK
            return true;
        }
    }
    return false;
};

The compiler says error: 'const class std::optional' has no member named 'insert_sample'.

I understand the error, but I have not been able to find a solution that works. If I write

*micro_cluster.insert_sample(sample, weight);

the compiler says error: 'const class std::optional' has no member named 'insert_sample'. I have also tried with emplace() and value().

I would appreciate if anyone can help me.

Okay, now it works. As suggested in the comments, I changed to

micro_cluster->insert_sample(sample, weight);

and the interface has to be

bool DenStream::_try_merge(const double sample,
                           const double weight,
                           std::optional<MicroCluster>& micro_cluster)

Thanks!

A Hansson
  • 77
  • 2
  • 9
  • 2
    You need to use the [deference operator](https://en.cppreference.com/w/cpp/utility/optional/operator*) to access the (optionally) contained variable instance. – G.M. Apr 08 '20 at 08:34
  • 1
    `*micro_cluster.insert_sample(sample, weight);` fails due to operator precedence. It should be either `(*micro_cluster).insert_sample(sample, weight);` or probably better `micro_cluster->insert_sample(sample, weight);` – UnholySheep Apr 08 '20 at 09:12
  • The compiler still complains. When I tried with `micro_cluster->insert_sample(sample, weight)` it says *error: passing 'const MicroCluster' as 'this' argument discards qualifiers [-fpermissive]* – A Hansson Apr 08 '20 at 09:15
  • 4
    That's a completely different error: you're trying to modify `micro_cluster` which has been declared `const`. – G.M. Apr 08 '20 at 09:16

0 Answers0