Go to Post You definitely know you've overdosed on first when you wash the aluminum shavings off your hands AFTER you eat dinner. - Maxpower57 [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 12-11-2012, 14:48
Greg McCoy's Avatar
Greg McCoy Greg McCoy is offline
boiler up!
FRC #3940 (CyberTooth)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 2002
Location: Kokomo, IN
Posts: 484
Greg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond reputeGreg McCoy has a reputation beyond repute
Send a message via AIM to Greg McCoy
Re: What does the ampersand (&) mean in errors?

I just saw your other thread - there are a lot of subtle differences between C++ and Java, and the ampersand is one of them. In your Java class...
Code:
// LimitSwitch.java
public class LimitSwitch {

    protected DigitalInput di;

    public LimitSwitch(int portNumber) {
        di = new DigitalInput(portNumber);
    }

    public boolean isOpen(){
        if (di.get() == true) {
            return true;
        } else return false;
    }

    public boolean isClosed(){
        return (!isOpen());
    }
}
...di is not a DigitalInput, it's actually a reference to a DigitalInput.

Unlike Java, in C++ object variables hold value types. In C++ there are references, which are sort of the equivalent to what you are used to in Java. The ampersand you're seeing indicates this reference type. For example, the const DigitalInput & in the DigitalInput constructor prototype means you should pass a reference to a DigitalInput object; not an actual DigitalInput object instance variable. This is conceptually similar to C-style pointers (which are also available in C++).

Here's how you might translate your Java class using a pointer to match the Java semantics:
Code:
// C++
#include "WPILib.h"

class LimitSwitch
{
	DigitalInput *di;

public:
	LimitSwitch(UINT32 p)  // constructor
	{
		di = new DigitalInput(p);
	}
	
	~LimitSwitch()  // destructor
	{
		delete di;
	}
	
	bool GetState()
	{
		return di->Get();
	}
};
There are a lot of other differences between C++ and Java; I'd recommend looking at the following resources:
Again, hope this helps
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 12:54.

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