Thursday, February 10, 2011

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

#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!

  • 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.

  • 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.

    4501 : error C2504: 'Singleton' : base class undefined
    ephemient : Okay, same thing. I should have known you were using MSVC from the error messages and `#pragma once` ... Anyhow, the problem may not be `Singleton.h` but it's definitely something included earlier. Post more sources and complete compiler output, please.
    From ephemient
  • 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;
    };
    
    From sbi
  • 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.

    From richj

0 comments:

Post a Comment