Tutorial :Is there anything wrong with the way that this C++ header is laid out?



Question:

#pragma once  #include "LudoCore/Singleton.h"    class LudoTimer : public Singleton<LudoTimer>  {      friend class Singleton<LudoTimer>;    public:      void Update();      void ShortenDay();      void LengthenDay();      UINT64 GetDeltaTime() const;      float GetPercentageOfDayElapsed() const;    private:      LudoTimer();      ~LudoTimer();        UINT64 GetTickCount64() const;      UINT64 GetElapsedSeconds() const;        UINT64 m_DeltaTime;        // Tick Count      UINT64 m_CurrFrameTick;      UINT64 m_LastFrameTick;        int m_SecondsInADay;      static const int SHORTEST_POSSIBLE_DAY = 60;      static const int LONGEST_POSSIBLE_DAY = 86400;      static const int CHANGING_INTERVAL = 600;  };  

To me, the above code looks normal. However, I'm new to C++ so I may be missing some nuance. I'm getting a bunch of compiler errors from it, such as:

error C2447: '{' : missing function header (old-style formal list?)

and

error C2236: unexpected 'class' 'LudoTimer'. Did you forget a ';'?

What gives!


Solution:1

Have a look in the other header (LudoCore/Singleton.h). The second error implies that the error is before the class LudoTimer declaration at the top.

My guess is that Singleton.h defines a class, and there's a missing ';' after that class definition.


Solution:2

The error is probably in LudoCore/Singleton.h or something else included earlier. Make sure your class definitions have ; semicolons after them and all that.

Quick test: comment out the #include and stick a template<class C> class Singleton; predeclaration there instead. If the compiler now complains about incomplete types, I'm right, and if not, post more details.


Solution:3

Well, the following compiles fine for me, so the error is very likely not in the code you showed us. I suggest you have a second look at Mike's suggestion that there is an error in Singleton.h.

//#include "LudoCore/Singleton.h"  #include <windows.h>    template< typename T >  class Singleton {};    class LudoTimer : public Singleton<LudoTimer>  {      friend class Singleton<LudoTimer>;  public:      void Update();      void ShortenDay();      void LengthenDay();      UINT64 GetDeltaTime() const;      float GetPercentageOfDayElapsed() const;  private:      LudoTimer();      ~LudoTimer();        UINT64 GetTickCount64() const;      UINT64 GetElapsedSeconds() const;        UINT64 m_DeltaTime;        // Tick Count      UINT64 m_CurrFrameTick;      UINT64 m_LastFrameTick;        int m_SecondsInADay;      static const int SHORTEST_POSSIBLE_DAY = 60;      static const int LONGEST_POSSIBLE_DAY = 86400;      static const int CHANGING_INTERVAL = 600;  };  


Solution:4

I was wondering if LudoTimer is declared at the point that it is used by Singleton and if a forward declaration would help? I didn't need one in VisualStudio 2005 and like sbi I could compile the code by supplying a declaration of Singleton. If I added a simplistic implementation I could even do:

    LudoTimer* timer = Singleton<LudoTimer>::instance();  

One more thing:

    error C2236: unexpected 'class' 'LudoTimer'. Did you forget a ';'?  

You could try adding a semicolon on a blank line after the #include to answer this question. If it helps then you can show that there is a problem in the header file without needing to edit it.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »