Go to Post Having the foolish belief that you can win a match in which you are completely outgunned will drive you to develop a winning strategy. - OZ_341 [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-11-2012, 19:42
JPruim JPruim is offline
Registered User
FRC #4014
 
Join Date: Mar 2012
Location: United States
Posts: 5
JPruim is an unknown quantity at this point
Custom Classes?

How would I go about creating a custom class? For example, I want a class that has a digitalInput encapsulated within it. In simple terms, How would I convert my existing LimitSwitch class (https://github.com/itguy51/FRC4014-2...mitSwitch.java) into C++? I have never actually used C++ in Object-Oriented programming other than simple things like in Visual Studio for demonstrations. I guessed how, and got this:
Code:
class limitSwitch
{
	DigitalInput di;
	limitSwitch::limitSwitch(UINT32 port){
		di(port);
	}
	bool limitSwitch::getState(){
		return di.Get() == 1;
	}
};
which is FULL of errors, there are more lines in the error log than there are in the cpp file. I get "Error: Expected ')' before "port"" and am confused because that makes no sense. I want to pass an argument to the constructor so that it assigns the DigitalInput to the initialized port.
getState is supposed to return true/false based on the state of the switch.
Reply With Quote
  #2   Spotlight this post!  
Unread 14-11-2012, 10:04
virtuald's Avatar
virtuald virtuald is online now
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,067
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Custom Classes?

Two ways of doing it. One is totally inline as you're doing it (which isn't really the C++ way of doing it).

Code:
#include <wpilib.h>

class LimitSwitch
{
public:

    LimitSwitch(UINT32 port) :
        di(port)
    {}
    
    bool getState()
    {
        return di.Get() != 0;
    }

private:
    DigitalInput di;
};
Or split into cpp/h files. Here's LimitSwitch.h:

Code:
#ifndef __LIMITSWITCH_H
#define __LIMITSWITCH_H

class LimitSwitch
{
public:

    LimitSwitch(UINT32 port); 
    
    bool getState();

private:
    DigitalInput di;
};
#endif
And LimitSwitch.cpp:

Code:
#include <wpilib.h>
#include "LimitSwitch.h"

LimitSwitch::LimitSwitch(UINT32 port) :
    di(port)
{}

bool LimitSwitch::getState()
{
    return di.Get() != 0;
}
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 14:22.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi