CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    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?

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    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);
    }

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    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.

  4. #4
    Join Date
    Jul 2010
    Posts
    75

    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)

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Jul 2010
    Posts
    75

    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.

  7. #7
    Join Date
    Apr 2008
    Posts
    725

    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.

  8. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Implementing a class

    Quote Originally Posted by paprica View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured