Help! Timer Function in WPIlib.h

I found this inside of the WPI libary

[http://first.wpi.edu/FRC/roborio/release/docs/cpp/classfrc_1_1Timer.html#a5f16e8da27d2a5a5242dead46de05d97](http://first.wpi.edu/FRC/roborio/release/docs/cpp/classfrc_1_1Timer.html#a5f16e8da27d2a5a5242dead46de05d97)

I put the line

Timer::Timer();

into the code.
Timer::Timer() means

Create a new timer object.

Create a new timer object and reset the time to zero. The timer is initially not running and must be started.

When I built the code in eclipse, it came back with this error >


error: cannot declare member function 'frc::Timer::Timer' within 'Robot'
  Timer::Timer();
               ^

I don’t know why its doing this. If you know please tell.

Thx.

Try using this instead: frc::Timer()

Error

expected unqualified-id before ')' token

Timer is not a function. It is an object. This should work:


#include <IterativeRobot.h>
#include <Timer.h>

class Robot: public frc::IterativeRobot {
public:
	void RobotInit() {
		timer = new frc::Timer();
	}

private:
	frc::Timer* timer;
};

START_ROBOT_CLASS(Robot)

And a more complete example:
https://github.com/wpilibsuite/EclipsePlugins/blob/master/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/GettingStarted/src/Robot.cpp

double Timer::Get()const

also a object that needs to be declared? If so how?
Thx.

If you have a timer defined, you can Get() what its time is. For example,


timer = new frc::Timer();

timer.Reset();
timer.Start();


double time = timer.Get();

Almost … in C/C++, the use of pointers to objects versus actual objects themselves makes a confusing difference of the use of ‘.’ (objects) or the use of ‘->’ (pointer to an object). So, with all that said, what’s the rule? Here’s an example of using a pointer as was intended above…


frc::Timer *pTimer; // Note the use of 'p' notation just to let the reader know this is a pointer...

pTimer = new frc::Timer;
// Now let's use the timer (it's a pointer so we use '->' and not '.'
pTimer->Reset();
pTimer->Start();

double timenow = pTimer->Get();
// Then eventually in your destructor you'd delete the pointer to "clean up"
delete pTimer;

The same piece of code but done with an object and NOT a pointer to an object would be…


frc::Timer MyTimer; // Note no 'p' preceeding - it's a real object - not a pointer. So there's no need to 'new' it either...

// Now let's use the timer (it's an object so we use '.')
MyTimer.Reset();
MyTimer.Start();

double timenow = MyTimer.Get();
// No need to delete. It'll go away when it goes out of scope.

Thanks bob.wolf68. The timers work but… I have encountered a situation which is on a new thread. https://www.chiefdelphi.com/forums/showthread.php?t=155295

Does anyone have a timer code that works like the one provided inside of WPIlib? The one in the WPI crashes the code every time :frowning: