Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Trouble modifying/replacing parts of WPIlib

  1. #1
    Join Date
    Oct 2008
    Posts
    17

    Default Trouble modifying/replacing parts of WPIlib

    For some more background information, here is a link to this same topic on chiefdelphi .

    What I'm trying to do is roughly outlined on page 76 in the C Programming Guide. I want to modify RobotDrive.cpp and RobotDrive.h in my SimpleRobot project. What I've done is copy these files from the WPIlib source project into my project, made the appropriate modifications, and then tried to compile. In modifying the files, I ended up commenting out all but three of the original functions and adding some stuff of my own (which I'm convinced is error-____). Consequently, when I go to build the project, those three functions have "multiple definitions errors".

    I am aware that I could just modify the WPIlib project and compile a custom WPIlib.a file, but that really isn't a lightweight option. I looked at page 76 in the C Programming Guide and it looked like what I tried ought to have worked out just fine.

    Can anyone out there tell me what I need to do to get this to work? I really don't want to go the custom WPIlib.a path, but I certainly could if there isn't a better alternative.

    Thanks,
    -Matt
    Last edited by manderson5192; 12-29-2008 at 11:03 PM.

  2. #2
    Join Date
    Oct 2008
    Location
    Las Vegas
    Posts
    18

    Default Re: Trouble modifying/replacing parts of WPIlib

    You should change the name of your custom 'RobotDrive' class. Just search and replace all occurrences of 'RobotDrive' in your new cpp and h file with something else like RobotDrive2, ImprovedRobotDrive, MyRobotDrive, etc.

    When the linker finds two different implementations of RobotDrive it doesn't know which one you want to use.

  3. #3
    Join Date
    Oct 2008
    Posts
    17

    Default Re: Trouble modifying/replacing parts of WPIlib

    Sure, I could do that. I just thought page 76 of the guide indicated that there was a better way than compiling a new WPILib.a file or creating a new class altogether. Oh well! I guess I'll get started copying my code over to new classes.

    Nonetheless, for the sake of posterity, does anyone really know what page 76 of the C Programming Guide is really talking about???

    Thanks!
    -Matt

  4. #4
    Join Date
    Aug 2008
    Location
    Manchester NH
    Posts
    84

    Default Re: Trouble modifying/replacing parts of WPIlib

    Matt -

    I didn't want to respond, since I haven't done C++ programming "for real" in 6 years. I was hoping someone with "actually knows what they are talking about" technology would reply.

    What I was hinting at is called inheritance. You can find tons of tutorials on it on the web. The basic premise is that your new class explicitly says "I am a child of RobotDrive", and gains all of the functions/variables of its parent. You may then overwrite or add to just the functions/variables that you want to change.

    - Eric

  5. #5
    Join Date
    Sep 2008
    Location
    National Instruments: Austin, TX
    Posts
    445

    Default Re: Trouble modifying/replacing parts of WPIlib

    Quote Originally Posted by EVanWyk View Post
    What I was hinting at is called inheritance. You can find tons of tutorials on it on the web. The basic premise is that your new class explicitly says "I am a child of RobotDrive", and gains all of the functions/variables of its parent. You may then overwrite or add to just the functions/variables that you want to change.
    It depends on exactly what you are trying to accomplish, but it is highly likely that subclassing the library class is the best approach to take for that kind of task.

  6. #6
    Join Date
    Oct 2008
    Posts
    17

    Default Re: Trouble modifying/replacing parts of WPIlib

    I've already re-written my code as a replacement class, but this idea of subclassing the library seems like a much much easier solution for all the other modifications I want to make to the WPILib code.

    I think I've done what you guys are talking about in AP Computer Science AB in Java, but I'm not really sure how to implement it in C++. To try to get a better picture of this, I've taken a quick peek into the WPILib source code. It looks like the way to indicate that a class is a subclass of another is to write "class ClassName: public ClassBeingSubclassed {...code here...}" in the respective .cpp and .h files. It also did a quick lookup on some of the C++ keywords used in WPILib where subclassing occurs and it looks like you have to put the keyword "virtual" in the definitions of functions (in the .h files) that you want to overwrite. Doesn't this mean that I have to modify my WPILib project's RobotDrive.h file so that the functions I want to overwrite when I subclass are "virtual void functionName(parameter)" and not just "void functionName(parameter)"?

    Thanks for your patience, guys! I'm learning lots here .

    -Matt

  7. #7
    Join Date
    Oct 2008
    Location
    Las Vegas
    Posts
    18

    Default Re: Trouble modifying/replacing parts of WPIlib

    Yes you have inheritance figured out. Class B : public A {...}; means that B is a subclass of A. Putting the virtual keyword on the member functions is how you ensure that the correct function will be called even if you use a base-class pointer type to call the function. For example:

    A * myobj = new B;
    myobj->Func(); // myobj is type A* and will call A's 'Func()' unless 'Func' is declared virtual

    You can do what you want without using virtual though *As long as you're explicitly using the correct type* for example:

    B * myobj = new B;
    myobj->Func(); // myobj is type B* and will call B's 'Func()' even if its not declared virtual


    Greg Hjelstrom
    Team 987
    Last edited by Hjelstrom; 12-31-2008 at 02:55 PM.

  8. #8
    Join Date
    Oct 2008
    Posts
    17

    Default Re: Trouble modifying/replacing parts of WPIlib

    Just to clarify, that means I need to recompile WPILib.a with the .h files modified such that the virtual keyword is before every member function that I want to overwrite?

    As an example of what I think I need to do:

    If I want to use my subclass's implementation of the deconstructor ~RobotDrive(), do I need to modify the header file such that the function definition reads "virtual ~RobotDrive();" and not just "~RobotDrive();"?

    Thanks!
    -Matt

  9. #9
    Join Date
    Sep 2008
    Location
    National Instruments: Austin, TX
    Posts
    445

    Default Re: Trouble modifying/replacing parts of WPIlib

    Quote Originally Posted by manderson5192 View Post
    Just to clarify, that means I need to recompile WPILib.a with the .h files modified such that the virtual keyword is before every member function that I want to overwrite?

    As an example of what I think I need to do:

    If I want to use my subclass's implementation of the deconstructor ~RobotDrive(), do I need to modify the header file such that the function definition reads "virtual ~RobotDrive();" and not just "~RobotDrive();"?

    Thanks!
    -Matt
    You don't need to modify the library to add virtual if you are not going to access your new subclass through a pointer to the base class. If you simply want to subclass it and create and use that subclass directly, you do not need to modify the library at all.

  10. #10
    stevethetiner Guest

    Default Re: Trouble modifying/replacing parts of WPIlib

    Can anyone tell me which type of project to create in wind river workbench for loading to the cRIO? I haven't been able to find any FRC samples, just WPI's samples that use a loadable kernel module.

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •