lundi 26 février 2018

compile error with std::reference_wrapper "has no member named" when used with class

So I've found this fancy stuff "std::reference_wrapper" in C++11 which I found could be useful in future program development. I played with it for a bit and I thought it could replace good-old reference in some cases for better usage, until I bumped into the following error:

#include <iostream>
#include <functional>

class testbench {
public:
    int ownerid = 5;
    bool camper = false;
};

class testing {
public:
    testing(testbench &x);
    int quack = 3;
    std::reference_wrapper <testbench> camper;
};

testing::testing(testbench &x):camper(x) {
}

int main() {
    testbench tempo;
    testing test_a (tempo);
    std::cout << test_a.camper.ownerid;
}

this would cause an error in gcc (I'm using Code::Blocks as Editor and gcc as compiler)

'class std::reference_wrapper<testbench>' has no member named 'ownerid'

If the line

    std::reference_wrapper <testbench> camper;

is changed to :

    testbench &camper;

Then everything is worked as I expected.

How do I fix this, or is it just my misunderstanding "std::reference_wrapper" as a general improvement of reference.

Aucun commentaire:

Enregistrer un commentaire