-
December 18th, 2010, 06:20 PM
#1
Implementing a class
I'm trying to make a class that will help me calculating the delta time in my game.
dt.h:
Code:
#include <ctime>
class dt
{
public:
void SetMaxFPS(int num);
void UpdateCurrentTicks();
void UpdateLastTicks();
long GetDeltaTime();
int GetFPS();
int GetMaxFPS();
bool UpdateNeeded();
private:
long GetTicks();
int max_fps;
int current_ticks;
int last_ticks;
};
dt.cpp:
Code:
#include "dt.h"
void dt::SetMaxFPS(int i)
{
max_fps = i;
}
void dt::UpdateCurrentTicks()
{
current_ticks = GetTicks();
}
void dt::UpdateLastTicks()
{
last_ticks = current_ticks;
}
long dt::GetTicks()
{
return clock();
}
long dt::GetDeltaTime()
{
return current_ticks - last_ticks;
}
int dt::GetFPS()
{
return (current_ticks - last_ticks) * 1000;
}
int dt::GetMaxFPS()
{
return max_fps;
}
bool dt::UpdateNeeded()
{
if(current_ticks > last_ticks + max_fps/1000)
return true;
return false;
}
main.cpp:
Code:
#include "dt.h"
int main(int argc, char* argv[])
{
dt::SetMaxFPS(100);
}
And I get the next error:
Code:
error C2352: 'dt::SetMaxFPS' : illegal call of non-static member function
What am I doing wrong?
-
December 18th, 2010, 06:29 PM
#2
Re: Implementing a class
you have to make an object dt:
Code:
#include "dt.h"
int main(int argc, char* argv[])
{
dt aDt;
aDt.SetMaxFPS(100);
}
-
December 18th, 2010, 06:29 PM
#3
Re: Implementing a class
The compiler put it pretty plainly. You are calling SetMaxFPS as if it was a static member, which it is not. Either make it static or instantiate an object of type dt.
-
December 18th, 2010, 07:21 PM
#4
Re: Implementing a class
I made it static and then I got this:
Code:
error C2597: illegal reference to non-static member 'dt::max_fps'
So I made max_fps a static int and now I get this:
Code:
error LNK2001: unresolved external symbol "private: static int dt::max_fps" (?max_fps@dt@@0HA)
-
December 18th, 2010, 09:32 PM
#5
Re: Implementing a class
The "make it static" suggestion is a mislead. While it's possible to do things that way, it probably isn't the *right* solution for this problem.
You have some fields in your dt class----last_ticks, etc. Where are these variables intended to live? You can either make them global, in which case you probably want dt to be a namespace rather than a class, or you can create a dt object to contain them. The latter is probably a better idea.
-
December 19th, 2010, 04:00 PM
#6
Re: Implementing a class
Thank you!
I have one more question,
how do I initialize a private int such as current_ticks to 0?
It wont let me initialize it to 0 in its definition.
-
December 19th, 2010, 05:26 PM
#7
Re: Implementing a class
use initializer list:
http://www.codeguru.com/forum/showthread.php?t=464084
e.g.
Code:
class Foo
{
public:
Foo(int x);
Foo();
int a;
};
Foo::Foo(int x) : a(x)
{
}
Foo::Foo() : a(0)
{
}
Last edited by Amleto; December 19th, 2010 at 05:31 PM.
-
December 21st, 2010, 12:43 PM
#8
Re: Implementing a class
Originally Posted by paprica
Thank you!
I have one more question,
how do I initialize a private int such as current_ticks to 0?
It wont let me initialize it to 0 in its definition.
You can set the private member in every member function. For initialisation the constructor(s) are the right place and the most recommended way is to using the initializer list as shown by Amleto. However, you also could set it in the body of the constructor.
Code:
dt::dt()
{
current_ticks = 0;
}
It is not recommended to set it only before first use in a member function cause any instantiated object of the class (after constructing) should have a proper initialisation of its members.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|