【问题标题】:Is there anything wrong with the way that this C++ header is laid out?这个 C++ 标头的布局方式有什么问题吗?
【发布时间】:2009-11-06 22:59:20
【问题描述】:
#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;
};

对我来说,上面的代码看起来很正常。但是,我是 C++ 新手,所以我可能会遗漏一些细微差别。我从中得到了一堆编译器错误,例如:

错误 C2447:“{”:缺少函数头(旧式正式列表?)

错误 C2236:意外的“类” 'LudoTimer'。你忘了一个';'吗?

什么给了!

【问题讨论】:

  • 这些错误不会恰好包含行号,对吧?
  • 另外,Singleton.h 可能很重要。如果不是太长,能不能也贴一下?

标签: c++


【解决方案1】:

查看另一个标题 (LudoCore/Singleton.h)。第二个错误意味着该错误在顶部的class LudoTimer 声明之前。

我的猜测是 Singleton.h 定义了一个类,并且缺少 ';'在该类定义之后。

【讨论】:

    【解决方案2】:

    错误可能在LudoCore/Singleton.h 或之前包含的其他内容中。确保您的 class 定义后面有 ; 分号等等。

    快速测试:注释掉 #include 并在此处粘贴 template&lt;class C&gt; class Singleton; 预声明。如果编译器现在抱怨类型不完整,我是对的,如果不是,请发布更多详细信息。

    【讨论】:

    • 好的,同样的事情。我应该从错误消息和#pragma once 中知道您正在使用MSVC ...无论如何,问题可能不是Singleton.h,但它肯定是前面包含的内容。请发布更多源代码和完整的编译器输出。
    【解决方案3】:

    嗯,下面的编译对我来说很好,所以错误很可能不在你展示给我们的代码中。我建议你再看看Mike's suggestionSingleton.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;
    };
    

    【讨论】:

      【解决方案4】:

      我想知道 LudoTimer 是否在 Singleton 使用它的时候声明了,前向声明是否有帮助?我在 VisualStudio 2005 中不需要它,并且像 sbi 一样,我可以通过提供 Singleton 的声明来编译代码。如果我添加一个简单的实现,我什至可以这样做:

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

      还有一件事:

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

      您可以尝试在#include 之后的空白行中添加分号来回答此问题。如果它有帮助,那么您可以证明头文件中存在问题,而无需对其进行编辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-14
        • 2014-08-31
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2012-12-04
        相关资源
        最近更新 更多