View unanswered posts | View active topics It is currently Sat Apr 21, 2018 9:09 pm

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Operator overloading for a struct
Author Message
Rookie

Joined: Fri Nov 22, 2013 5:40 pm
Posts: 2
Operator overloading for a struct
Is it possible to do operator overloading in RobotC? For example we have a struct:
 Code:typedef struct{float x;float y;float z;} vector;
And we would like to be able to run:
 Code:vector a, b;a.x = 1;a.y = 2;a.z = 3;b=a;
And have all the x,y,z values copied over. Is this possible in RobotC?

Thanks in advance,

- Alex

Fri Nov 22, 2013 5:47 pm
Professor

Joined: Sat May 18, 2013 1:24 pm
Posts: 271
Location: Olympia, WA
Re: Operator overloading for a struct
No, I don't think this is possible in RobotC (or ANSI C, even).

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!

Fri Nov 22, 2013 6:52 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Operator overloading for a struct
lemiant wrote:
Is it possible to do operator overloading in RobotC? For example we have a struct:
 Code:typedef struct{float x;float y;float z;} vector;
And we would like to be able to run:
 Code:vector a, b;a.x = 1;a.y = 2;a.z = 3;b=a;
And have all the x,y,z values copied over. Is this possible in RobotC?

Thanks in advance,

- Alex

That's not operator overloading. That's just structure assignment. This should be allowed. Since you already have the code written, why can't you just compile the code and check it out. To verify if it indeed works, add some nxtDisplayText statements in the code such as:
 Code:typedef struct{    float x;    float y;    float z;} vector;task main(){    vector a, b;    a.x = 1.0;    a.y = 2.0;    a.z = 3.0;        b = a;        nxtDisplayTextLine(1, "b.x=%f", b.x);    nxtDisplayTextLine(2, "b.y=%f", b.y);    nxtDisplayTextLine(3, "b.z=%f", b.z);        while (true)    {        wait1Msec(100);    }}

Sat Nov 23, 2013 8:17 am
Rookie

Joined: Fri Nov 22, 2013 5:40 pm
Posts: 2
Re: Operator overloading for a struct
Good idea with the testing. It seems that structs can be passed as parameters to functions, but you cannot do a=b as I was hoping. Does that make sense?

Sat Nov 23, 2013 3:08 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Operator overloading for a struct
Hmm, you are right. It looks like structure assignment doesn't work. Then try this:
 Code:typedef struct{    float x;    float y;    float z;} vector;task main(){    vector a, b;    a.x = 1.0;    a.y = 1.0;    a.z = 1.0;        while (true)    {        a.x += 1.0;        a.y += 1.0;        a.z += 1.0;        memcpy(&b, &a, sizeof(a));//        b = a;            nxtDisplayTextLine(1, "b.x=%f", b.x);        nxtDisplayTextLine(2, "b.y=%f", b.y);        nxtDisplayTextLine(3, "b.z=%f", b.z);            wait1Msec(100);    }}

Sat Nov 23, 2013 4:51 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 5 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.