Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Declaring DoubleSolonoid (http://www.chiefdelphi.com/forums/showthread.php?t=133668)

RobHammann 29-01-2015 18:02

Declaring DoubleSolonoid
 
I have been trying to get solenoids to work all day, everything I try kicks back an error

grabSolenoid = new DoubleSolenoid(1, 1, 1);

always says "grabSolenoid is not a class"

What is the proper way to declare and run double solenoids? The screensteps isn't helping.

Jon Stratis 29-01-2015 18:59

Re: Declaring DoubleSolonoid
 
Try:

DoubleSolenoid grabSolenoid = new DoubleSolenoid(1, 1, 1);

RobHammann 02-02-2015 10:48

Re: Declaring DoubleSolonoid
 
Quote:

Originally Posted by Jon Stratis (Post 1435822)
Try:

DoubleSolenoid grabSolenoid = new DoubleSolenoid(1, 1, 1);

Could not convert '(operator new(104u), (<statement>, ((DoubleSolenoid*) <anonymous>)))' from' 'DoubleSolenoid*' to 'DoubleSolenoid'

GeeTwo 02-02-2015 11:59

Re: Declaring DoubleSolonoid
 
Oh, yeah, C++ forum...try:

DoubleSolenoid *grabSolenoid = new DoubleSolenoid(1, 1, 1);

Jon Stratis 02-02-2015 12:13

Re: Declaring DoubleSolonoid
 
Oops, my bad... The syntax is almost identical between java and c++, expert for that annoying *. I really hate dealing with punters in c++!

They issue you were having was that you hadn't declared your variable before assuming it, so the compiler didn't know what "grabSolenoid" was. By adding the class in front, you're telling the compiler what that is.

one_each 02-02-2015 12:27

Re: Declaring DoubleSolonoid
 
Here is a bit of explenation.

Could not convert '(operator new(104u), (<statement>, ((DoubleSolenoid*) <anonymous>)))' from' 'DoubleSolenoid*' to 'DoubleSolenoid'

The bolded parts are the important notes. Note the asterisk (or 'star') in the from and also note that it is missing in the to.

The new operator creates a new version of the class (DoubleSolenoid here) and returns a pointer to it. The asterisk (or 'star') indicates changes the type of grabSolenoid to a "pointer to DoubleSolenoid", thus making the types on both sides the same.

Pointers are one of the more confusing aspects of C/C++, however they are used everywhere. Remember, the pointer doesn't hold the data, it just points to where it is at. For classes, use the -> operator to access methods and fields from the pointer.

RobHammann 02-02-2015 16:03

Re: Declaring DoubleSolonoid
 
Thanks, they do declare now, however...

Quote:

Originally Posted by one_each (Post 1437283)
For classes, use the -> operator to access methods and fields from the pointer.


I really don't understand these pointers, when I try to set the solenoids using something like

grabSolenoid.set(DoubleSolenoid::kForward);

it says "request for member 'set' in '(((Robot*)this))->Robot::grabSolenoid', which is of pointer type 'DoubleSolenoid' (maybe you meant to use '->' ?)"

where do I put the ->?

Steve Warner 02-02-2015 16:14

Re: Declaring DoubleSolonoid
 
Try grabSolenoid->Set(DoubleSolenoid::kForward);

Alan Anderson 02-02-2015 16:20

Re: Declaring DoubleSolonoid
 
Quote:

Originally Posted by RobHammann (Post 1437387)
...using something like

grabSolenoid.set(DoubleSolenoid::kForward);

it says "request for member 'set' in '(((Robot*)this))->Robot::grabSolenoid', which is of pointer type 'DoubleSolenoid' (maybe you meant to use '->' ?)"

where do I put the ->?

You have declared grabSolenoid as a pointer to a double solenoid object. The member functions like Set() only work on an actual object. You need to dereference the pointer before you can call the function. The '->' is a small bit of syntax convenience that lets you dereference the pointer and do the equivalent of a '.' at the same time.

Just replace the '.' with '->' between the variable name and the function name.

GeeTwo 02-02-2015 17:08

Re: Declaring DoubleSolonoid
 
Quote:

Originally Posted by Alan Anderson (Post 1437394)
Just replace the '.' with '->' between the variable name and the function name.

Yes. obj_ptr->elt is equivalent to (*obj_ptr).elt . That is, look up the actual field or method within the object that obj_ptr points to.

Joe Ross 02-02-2015 18:23

Re: Declaring DoubleSolonoid
 
Quote:

Originally Posted by RobHammann (Post 1435800)
grabSolenoid = new DoubleSolenoid(1, 1, 1);

Once you get the syntax correct, you'll want to make sure you're allocating unique channels, as you'll likely get a runtime error as written.

RobHammann 04-02-2015 15:46

Re: Declaring DoubleSolonoid
 
Well it seems to have worked, I cant test it yet on actual solenoids, but it builds and deploys just fine


All times are GMT -5. The time now is 12:07.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi