![]() |
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)':Code:
#include "WPILib.h" |
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"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){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{In this case, no such operator is defined in DigitalInput..and, so, it doesn't know what to do. |
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"Hope this helps! Here are some links for more information: |
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.javaUnlike 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++
Again, hope this helps :] |
| All times are GMT -5. The time now is 18:21. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi