Hello BB community,
I noticed a strange error while I was programing in C++.
When I do this:
void function()
{
}
void main()
{
run_for(120, function());
}
In the original "C", there is no error, but once I copy paste it into "C++" and compile it, I get "invalid use of void expression."
Am I missing something?
Thanks,
- Ulzee
run_for accepts a pointer to a void function. When you call function(), you are giving run_for the void that function() returns, not a pointer to the function. The correct usage is:
run_for(120, function);
I have no idea why it works for you in C; I assume that gcc is automatically typecasting the argument while g++ is more strict about this.
-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS
Thanks for the comments,
I tried removing the () in g++, but it now gives me an error:
error: invalid conversation from 'void(*)()' to 'void*'
error: initializing argument 2 of "void run_for(float,void*)"
p.s. it's my first time coding for Botball in g++, and I want to use mrp that I used in g; it occurs to me it doesnt work in the same way; how do you use it? I can't seem to make sense of the Target Manual.
This is a brief code I have:
#include
// Stacker Claw uses motor 3
void close_stacker()
{
Port(3);
moveRelativePosition(500,-310);
}
Thanks,
- Ulzee
Hmm, I recall Matt Thompson mentioning to me that KIPR messed up with void pointers... I think this is a bug in KIPR's code. KIPR is misusing a void pointer to mean a pointer to a void function. I haven't tried this, but you could see if this works properly:
run_for(120, (void*)function);
Even if it compiles, I'm not 100% sure that it will work as intended... you should test it and make sure that it works before using in a mission-critical setting. You might also want to notify KIPR about the bug.
Let us know if it works!
-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS
Also, I'm not familiar with KIPR's C++ wrapper of libcbc, but if they were competent the C versions should work under C++ as well. (Note that I make no guarantees that KIPR was competent.)
-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS
This is indeed an issue with libcbc. libcbc was not programmed with C++ in mind, so it was able to get away with some implicit casting.
Here is a wrapper function for a temporary work-around.
This will be corrected in the next CBC firmware (which might not coincide with the next IDE update.)
Braden McDorman
Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.
Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.