Type mismatch using CC_CALLBACK

I’ve a problem with code analysis write some simple functions in C++.

This is a sample block:

and this is the error reported by CLion using CMake v3.3.2

"Parameter type mismatch: Expression must be rvalue"

I really don’t know what’s the problem.
If I compile with Visual Studio it’s all ok, but with CMake I got this.

I have already set the -fpermissive flag.

This occurs with all CC_CALLBACK and I noticed that if I change this with &this it probably works, but I don’t know if I have to change this or not.

Please help me, thank you.

I can’t help you with your problem, but if you just want a quick fix you could bypass the error by using a lambda instead of CC_CALLBACK_2.

I presume you’re code is within a non-static method on the class Start, yes? If not then the type of this is invalid.
Does onKeyReleased have the function declaration of void (EventKeyboard::KeyCode code, Event* event)?

Thank you for the answers

@cei please can you make an example on how can I write a lambda with that function?

@stevetranby yes my method is not static and the declaration is like you wrote:

I’ve followed various examples, and a lot of codes are written in this way.
The strange thing is that on VS there’s no error and the build start.

Yeah, it seems that VS is less strict with the syntax. I started my first project in VS and all compiled fine, then I moved to Xcode and oh dear, so many errors popped up.

As for the code you posted, you missed the override keyword after bool init(), and the virtual before it is needed only if you subclass Start. Try fixing it, maybe that’s what causing the error.
(The function becomes bool init() override;).

In short you write lambdas as

[variables to capture](requested arguments if needed) -> returnType //If the return type is `void` you can skip the ` -> void`
{
    your function's body
}

The reason for capturing variables is that when you have a function that has one or more local variables and creates the lambda, and inside the lambda you need to use those variables, generally when the lambda is executed they are gone, since the method has ended. The way around this is capturing the needed values inside the square brackets.
This is just a very simple explanation, and I skipped some details.
Remember that lambdas might turn out to be really, really, really useful sometimes, and it’s always good to know how they work and how to use them. Check out this great c++ tutorial about lambdas for more.

in your case the lambda is

[](EventKeyboard::KeyCode keyCode, Event *event)
{
    //Function body
}

Probably a CLion bug then? If VS is compiling it correctly. Can you run cpp-tests with either or both CLion and VS?

Thank you @cei I got it.
I generally use lambda function with addTouchEventListener, it’s really useful.

For some other functions I prefer to declare a function a part, just for legibility.
Anyway with your suggestion, obviously it works.

I’ve tried also with override in the init (thank you for suggestion) and removing virtual, but the error remain.

Maybe have I set some flags to cmake?

@stevetranby now I try to run cpp-tests with CLion, in case of fail maybe that is something related to bad cmake configuration I think.

The cpp-tests runs fine under VS, now I’m trying with cmake but with -G"Visual Studio 12 2013" options, I’ve read on stack overflow that this could help… and also with -fpermissive

I feel like -fpermissive is not a great “tool” to use? Doesn’t it essentially mask or hide real issues. I guess if you must still use VS2013 it may be the case you need it in certain instances, but we use CC_CALLBACK, SEL_SELECTOR, and lambdas and VS2015 doesn’t complain … not saying you’re doing anything wrong, just some feedback.

Anyway, since I’m not helping much I’ll just ask one quick question. Have you tried creating a new project, setting up a single button using CC_CALLBACK_2 and checking if that compiles/works fine in CLion and VS2013?

You’re right about -fpermissive, it’s not a good choice but it works.

With a new project it’s the same, VS ok, both 2013 and 2015, CLion same error.
I’ve tried to complete recreate a project, using the step by step guide I’ve made here, with latest suggestions from @Alexander_Boriskov [Guide] Compiling Cocos2d-x with CLion

Now I can run my project flawlessly in CLion with CMake, it works, only got the problem using CC_CALLBACK.

But in the meantime I’m trying to use cocos v3.10, and I’ve seen the prebuilt feature that speed up the building process a lot.
The prebuilt libs are only for VS, so I think that I’ve to follow this path, even if I don’t like it.
For sure in future I can’t use the prebuilt with CLion.
Maybe I have to switch to resharper from Jetbrains, that I can use in VS for a more comfortable way to write code.

I started all this for the debug feature from Jetbrains that I prefer, but I can’t wait 30-40 seconds for every build in cmake :stuck_out_tongue: so this time Microsoft wins :stuck_out_tongue:

BTW. Anyway I think that VS use some kind of “flag” that is more permissive like the -fpermissive used in cmake, but my knowledge of this type of building process are few.

Thank you @stevetranby for your answers.

If you figure out prebuilts in CLion let me know. I don’t think I’ll end up using it, but I’ve been keeping an eye on it and testing every few months.

I thought I was running into this issue, but mine was different, and my own mistake. In my searches for answers I stumbled upon this report below. (I might add that “expression must be rvalue” seems to be a very atypical compiler error, normally it would be “expression must be lvalue”)

per jet brains trac issue CPP-5004: (forum won’t let me link to the report - but maybe try putting this manually in your browser: youtrack.jetbrains.com/issue/CPP-5004 )

CPP-5004 False error: Parameter mismatch expression must be rvalue on constructing std::thread
Is duplicated by: CPP-5075 CPP-5428
Relates to: CPP-5125 CPP-3615
Parent for: CPP-5075 CPP-5428
Subtask of: CPP-1644
#include

void foo() {}

int main() {
auto t = std::thread(foo);
}
The error says “Parameter type mismatch: Expression must be rvalue”. However, this is valid C++ code, it compiles and runs as expected.