View Single Post
  #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