Using push_back of vector to report error in lambda expression
-
wrote on 16 Nov 2020, 09:23 last edited by
std::vector<int> ii; connect(but1, &QPushButton::clicked, this, [&]() { ii.push_back(1); });
breakdown when i push the button. qvector is the same.
-
std::vector<int> ii; connect(but1, &QPushButton::clicked, this, [&]() { ii.push_back(1); });
breakdown when i push the button. qvector is the same.
@monkey233 more information please, is
ii
local scope or a class member ? -
wrote on 16 Nov 2020, 09:35 last edited by
@monkey233 said in Using push_back of vector to report error in lambda expression:
Thank you for your reply. I'm a novice.sorry I don't know what you mean.i think ii is vector<int>. These three sentences together are correct when compiling. I omitted the statement of creating button.
-
@monkey233 said in Using push_back of vector to report error in lambda expression:
Thank you for your reply. I'm a novice.sorry I don't know what you mean.i think ii is vector<int>. These three sentences together are correct when compiling. I omitted the statement of creating button.
wrote on 16 Nov 2020, 09:42 last edited by@monkey233
So if it's exactly as you show,ii
is a local variable to the method you are in. That means it goes out of scope (i.e. gets destroyed) when you exit that function. Meanwhile, you have a lambda-slot attached to the signal which still references that variable, which now does not exist. Kaputt....Presumably, if you want that to do anything permanent/meaningful,
std::vector<int> ii
needs to be a member variable in your class, so that it persists. -
wrote on 16 Nov 2020, 10:45 last edited by
thank you,i will try
1/5