IDE C++

6 replies [Last post]
SEAL491
SEAL491's picture
Title: NooBot+
Joined: 07/19/2011
Posts:
BotPoints: 54
User offline. Last seen 10 years 47 weeks ago.

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

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
Posts:
BotPoints: 1168
User offline. Last seen 8 years 1 week ago.

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

SEAL491
SEAL491's picture
Title: NooBot+
Joined: 07/19/2011
Posts:
BotPoints: 54
User offline. Last seen 10 years 47 weeks ago.

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*)"

SEAL491
SEAL491's picture
Title: NooBot+
Joined: 07/19/2011
Posts:
BotPoints: 54
User offline. Last seen 10 years 47 weeks ago.

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

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
Posts:
BotPoints: 1168
User offline. Last seen 8 years 1 week ago.

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

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
Posts:
BotPoints: 1168
User offline. Last seen 8 years 1 week ago.

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

Beta
Beta's picture
Title: The Magnificent
Joined: 02/24/2012
Posts:
BotPoints: 266
User offline. Last seen 9 years 15 weeks ago.

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.

  1. void runFor(int time, void (*func)())
  2. {
  3. run_for(time, reinterpret_cast<void*>(func));
  4. }
  5.  
  6. ...
  7.  
  8. runFor(120, function);

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.