|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
What does the ampersand (&) mean in errors?
I get the error stack as follows when trying to compile the following code:
Code:
C:/Users/Josh/WindRiver/workspace/SimpleTemplate/limitSwitch.cpp: In constructor `limitSwitch::limitSwitch(UINT32)': C:/Users/Josh/WindRiver/workspace/SimpleTemplate/limitSwitch.cpp:8: error: no matching function for call to `DigitalInput::DigitalInput()' C:/WindRiver/vxworks-6.3/target/h/WPILib/DigitalInput.h:21: note: candidates are: DigitalInput::DigitalInput(const DigitalInput&) C:/WindRiver/vxworks-6.3/target/h/WPILib/DigitalInput.h:24: note: DigitalInput::DigitalInput(UINT8, UINT32) C:/WindRiver/vxworks-6.3/target/h/WPILib/DigitalInput.h:23: note: DigitalInput::DigitalInput(UINT32) C:/Users/Josh/WindRiver/workspace/SimpleTemplate/limitSwitch.cpp:9: error: no match for call to `(DigitalInput) (UINT32&)' C:\WindRiver\vxworks-6.3\host\x86-win32\bin\make.exe: *** [SimpleTemplate_partialImage/Debug/Objects/SimpleTemplate/limitSwitch.o] Error 1 Build Failed in Project 'SimpleTemplate' (Process Exit Value was 2): 2012-11-10 18:06:08 (Elapsed Time: 00:02) Code:
#include "WPILib.h"
class limitSwitch
{
DigitalInput di; // robot drive system
public:
limitSwitch(UINT32 p){
di(p);
}
};
|
|
#2
|
|||
|
|||
|
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 ^^
}
};
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++;
}
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) 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;
}
};
In this case, no such operator is defined in DigitalInput..and, so, it doesn't know what to do. |
|
#3
|
||||
|
||||
|
Re: What does the ampersand (&) mean in errors?
Initialization in C++ is a little tricky when you're using objects as member variables.
When you declare the limitSwitch(UINT32) constructor, the compiler is trying to use the default constructor to create the di object, before it even looks at your constructor body. The DigitalInput class doesn't have a default (or zero-argument) constructor, so the compiler is giving you a list of the constructors which are available. You can tell the compiler to use another constructor by using an "initialization list." I think what you mean to do is this: Code:
#include "WPILib.h"
class limitSwitch
{
DigitalInput di; // robot drive system
public:
limitSwitch(UINT32 p) : di (p) { }
};
Hope this helps! Here are some links for more information: |
|
#4
|
||||
|
||||
|
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());
}
}
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();
}
};
Again, hope this helps ![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|