View Single Post
  #2   Spotlight this post!  
Unread 11-11-2012, 17:58
phynix phynix is offline
Registered User
FRC #3130 (ERRORs)
Team Role: Programmer
 
Join Date: Sep 2012
Rookie Year: 2012
Location: Minnesota
Posts: 16
phynix is an unknown quantity at this point
Re: What does the ampersand (&) mean in errors?

You have a problem in how you're initializing the variable di. In C++, you an only initialize variables and objects once--generally at the start of the constructor. You have the general idea right, but not the implementation. Try this instead:

Code:
#include "WPILib.h"

class limitSwitch
{
	DigitalInput di; // robot drive system

public:
	limitSwitch(UINT32 p) : di(p){
		//look at above line ^^
	}
};
Regarding the ampersand (&).

This is C++ syntax--not something specific to error messages.

When you declare a function (or constructor, for that matter), you have a couple of options in how you want to handle input parameters.

Code:
void increment(int& i){
	i++;
}
^^This code will increment an integer. the &, in this case, means you're passing the value by reference. Whatever you do to this value will occur to the actual variable in whatever code you use.

Likewise, every class has a default copy constructor--whether or not it is declared. In the case of the DigitalInput class, the copy constructor is defined [by the compiler] as:

Code:
DigitalInput::DigitalInput(const DigitalInput& somevalue)
Of course, the variable name (somevalue, as I put), doesn't matter--so the compiler doesn't print it out.

The copy constructor creates a copy of the input DigitalInput.

So, that's all the ampersand is doing in this case.

In the second part, no match for call to `(DigitalInput) (UINT32&)'

I'm not exactly sure what's going on--but what you're trying to do _is_ illegal. You can't initialize a variable like that.

Based on what I'm seeing, it looks like 1) it's trying to cast the integer to a DigitalInput (which is completed through the use of a compiler-defined copy constructor), or 2) it's trying to call a copy constructor on DigitalInput itself, trying to pass the integer as a value.


On a side note....

Using C++ is is also possible to make a class be able to be cast to another class or variable, using the 'operator' functions.

For example, in a class, you could define a 'function' as:

Code:
class MyClass{
public:
	int value;
	operator double(double input){
		value=double;
	}
};
What you can then do is say MyClass c = 42.4. And it will set 'value' to 42.4.

In this case, no such operator is defined in DigitalInput..and, so, it doesn't know what to do.
Reply With Quote