【问题标题】:#including a class header does not specify type#包括一个类头没有指定类型
【发布时间】:2011-09-05 00:04:41
【问题描述】:

我在我的 wxWidgets 应用程序中遇到了一个非常奇怪的问题。我想要做的是使用自定义 wxPanel 资源来允许一些冗余控制并提供让我更轻松地工作的方法。在我尝试将对主框架中的资源的访问权传递给每个面板之前,此问题并未发生。

我正在做的是使用#include 将wxPanel 资源的类头包含在主类的头中。但是,当尝试声明 CopyRow 类型的资源时,它位于我包含的头文件中,我收到错误 CopyRow does not name a type

这是主类头的代码,

#ifndef CPAMOUNTMAIN_H
#define CPAMOUNTMAIN_H

//(*Headers(CPAmountFrame)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/menu.h>
#include <wx/spinctrl.h>
#include <wx/statline.h>
#include "CopyRow.h"
#include <wx/panel.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)

class CPAmountFrame: public wxFrame
{
    public:

        CPAmountFrame(wxWindow* parent,wxWindowID id = -1);
        void UpdateTotal();
        virtual ~CPAmountFrame();

    private:

        //(*Handlers(CPAmountFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OntotalCopiesChange(wxSpinEvent& event);
        static void CallTotalCopies();
        //*)

        //(*Identifiers(CPAmountFrame)
        static const long ID_CUSTOM1;
        static const long ID_CUSTOM2;
        static const long ID_CUSTOM3;
        static const long ID_CUSTOM4;
        static const long ID_CUSTOM5;
        static const long ID_CUSTOM6;
        static const long ID_STATICLINE1;
        static const long ID_STATICTEXT1;
        static const long ID_SPINCTRL1;
        static const long ID_STATICTEXT2;
        static const long ID_PANEL1;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(CPAmountFrame)
        CopyRow* Custom4;
        wxStaticText* totalPrice;
        CopyRow* Custom1;
        CopyRow* Custom5;
        CopyRow* Custom2;
        CopyRow* Custom3;
        wxPanel* Panel1;
        wxStaticText* StaticText1;
        wxStatusBar* StatusBar1;
        wxStaticLine* StaticLine1;
        CopyRow* Custom6;
        wxSpinCtrl* totalCopies;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // CPAMOUNTMAIN_H

这里是 CopyRow.h 的代码,

#ifndef COPYROW_H
#define COPYROW_H

#ifndef WX_PRECOMP
    //(*HeadersPCH(CopyRow)
    #include <wx/sizer.h>
    #include <wx/stattext.h>
    #include <wx/panel.h>
    //*)
#endif
//(*Headers(CopyRow)
#include <wx/spinctrl.h>
//*)
#include "CPAmountMain.h"

class CopyRow: public wxPanel
{
    public:

        CopyRow(wxWindow* parent,const char* label,wxSpinCtrl* copies,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);
        void SetLabel(const char* label);
        void SetPrice(double price);
        void SetCounter(int value);
        int  GetCounter();
        virtual ~CopyRow();

    private:

        //(*Declarations(CopyRow)
        wxStaticText* copyLabel;
        wxSpinCtrl* numCopies;
        wxStaticText* copyPrice;
        //*)

        //(*Identifiers(CopyRow)
        static const long ID_SPINCTRL1;
        static const long ID_STATICTEXT1;
        static const long ID_STATICTEXT2;
        //*)

        wxSpinCtrl* totalCopies;

        //(*Handlers(CopyRow)
        void OnnumCopiesChange(wxSpinEvent& event);
        //*)
        DECLARE_EVENT_TABLE()
};

#endif

谁能向我解释这个错误?我现在不知道。

【问题讨论】:

  • 这是您得到的唯一错误吗?你确定它在cpamountmain.h 吗?它在哪条线上?发布错误很好,但您需要发布所有错误(如果不是唯一错误)加上错误的确切文本,而不仅仅是错误(即包括文件和行信息)。
  • @Seth 它位于主标题的第 60、62、63、64、65 和 70 行。基本上在任何地方都声明了 CopyRow 类型的东西。
  • 还有其他错误吗?仔细看。

标签: c++ wxwidgets


【解决方案1】:

您不能在 CopyRow.h 中包含 CPAMountMain.h,在 CPAMountMain.h 中包含 CopyRow.h!您必须确定要包含文件的顺序。

由于 CPAMountMain.h 仅使用 指针 指向 CopyRow 类,您可以使用前向声明而不包括 CopyRow.h:

// CPAMountMain.h
class CopyRow;

并删除 CPAMountMain.h 中的 #include "CopyRow.h",这应该可以工作。

【讨论】:

  • 或从 CopyRow.h 中删除 include CPAmountMain.h
  • 或者确保始终首先包含CopyRow.h,但这是一个糟糕的解决方案。 +1
  • @Mat 是的,实际上好多了!
  • 这可行,但只能通过删除 CopyRow 中的包含。当我使用您所说的前向声明时,在实际实例化变量时,我在 CopyRow 的构造函数周围遇到了各种错误。我对 C++ 很陌生,正在构建这个项目以了解更多关于它的信息,但这是我不明白的。不过非常感谢。
  • @Matt 如果使用前向声明,则必须在 CPAmountMain.cpp 文件中包含 CopyRow 标头。
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2014-05-18
  • 2013-08-30
  • 2012-01-04
  • 1970-01-01
  • 2012-02-19
  • 2017-01-02
  • 1970-01-01
相关资源
最近更新 更多