how to avoid error “initial value of reference to non-const must be an lvalue” without…
- User
how to avoid error “initial value of reference to non-const must be an lvalue” without make it static
void RGNCore::signIn() { std::string redirectUrl = _applicationId + “://”; std::string url = “https://development-oauth.ready.gg/?url_redirect=” + redirectUrl + “%2F&returnSecureToken=true&appId=” + _applicationId; OSAdapter::OpenURL(url.c_str()); DeepLinkAdapter::AddListener(&onDeepLink); }
void RGNCore::onDeepLink(std::string payload) {
}
- Assistant
To avoid the "initial value of reference to non-const must be an lvalue" error without making the function static, you can do the following:
- Change the
onDeepLinkfunction declaration to a static member function:
cppstatic void onDeepLink(std::string payload);- Define the
onDeepLinkfunction outside of the class declaration:
cppvoid RGNCore::onDeepLink(std::string payload) { // Implementation }By making the
onDeepLinkfunction static, you remove the need for a non-const reference as a member function can only access static member variables directly. This way, you will avoid the error and still maintain the non-static nature of your function. - Change the
- User
but how to fix it without make it static
- Assistant
If you want to fix the error without making the function
onDeepLinkstatic, you can use a lambda function as a callback instead. Here's an example:cppvoid RGNCore::signIn() { std::string redirectUrl = _applicationId + "://"; std::string url = "https://development-oauth.ready.gg/?url_redirect=" + redirectUrl + "%2F&returnSecureToken=true&appId=" + _applicationId; OSAdapter::OpenURL(url.c_str()); // Define the lambda function as the callback auto callback = [](std::string payload) { // Implementation of the callback function }; // Pass the lambda function as the listener DeepLinkAdapter::AddListener(callback); }In this approach, instead of directly passing the
onDeepLinkfunction as the callback, you define a lambda functioncallbackthat captures any variables it needs and implement the desired functionality inside the lambda function. Then, you pass the lambda function as the listener to . This way, you can avoid the need for a non-const reference and fix the error without making the function static. - User
how to fix an error "error: expected '(' for function-style cast or type construction" here auto it = std::find_if(_listeners.begin(), _listeners.end(), [&listener](const auto& element) { return element.target<void(std::string)>() == listener.target<void(std::string)>(); });
- Assistant
The error "expected '(' for function-style cast or type construction" indicates that there is an issue with the type deduction or declaration in the lambda's
std::find_ifstatement. To fix this error, you can provide explicit template arguments to help the function deduce the correct type. Here's an example:cppauto it = std::find_if(_listeners.begin(), _listeners.end(), [&listener](const auto& element) { return element.target<void(*)(std::string)>() == listener.target<void(*)(std::string)>(); });In the lambda expression,
void(*)(std::string)specifies the function pointer type that thetargetfunction should expect. By providing this explicit template argument, you are helping the compiler correctly deduce the type and resolve the error.Make sure that
listenerandelementhave compatible types for comparison, such as both being of typestd::function<void(std::string)>or function pointers. Adjust the types as needed in the comparison expression within the lambda. - User
that doesn't help
- Assistant
Apologies for the confusion. To address the error, you can try using the
std::type_identityhelper to explicitly specify the lambda's return type. Here's an updated version of the code:cppauto it = std::find_if(_listeners.begin(), _listeners.end(), [&listener](const auto& element) -> std::type_identity_t<decltype(element)> { return element.target<void(*)(std::string)>() == listener.target<void(*)(std::string)>(); });By specifying
-> std::type_identity_t<decltype(element)>after the lambda's argument list, you provide the return type explicitly, which can help resolve the error.Ensure that both
listenerandelementhave compatible types for comparison, such asstd::function<void(std::string)>or function pointers. Adjust the types accordingly in the comparison expression within the lambda.