【问题标题】:C++ build error: class does not name a typeC++ 构建错误:类没有命名类型
【发布时间】:2016-11-26 09:18:07
【问题描述】:

我重新排列了一些代码,现在出现以下错误:

g++ main.cpp myframe.cpp `wx-config --cxxflags --libs std` -o main
myframe.cpp:5:1: error: ‘Myframe’ does not name a type

我很确定错误与包含有关,而不是与错误代码有关

这里是源文件(仅相关部分):

main.cpp:

#include "main.h"
#include "myframe.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit(){  
    Myframe *menu = new Myframe(wxT("Application"));
    menu->Show(true);
    return true;
};

main.h:

#include <wx/wx.h>

class MyApp : public wxApp
{
  public:
    virtual bool OnInit();
};

myframe.cpp:

#include "main.h"

Myframe::Myframe(const wxString& title) 
/// ^ ERROR
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 300))
{
  ...
}

myframe.h:

#include <wx/wx.h>
#include <wx/menu.h>

class Myframe : public wxFrame
{
public:
    Myframe(const wxString& title);

    ...
};

...(function definitions,event table and enums)

【问题讨论】:

  • 你需要#include "myframe.h" in myframe.cpp
  • @songyuanyao。这将导致 Myframe 的多个定义,因为它已经包含在 main.cpp ...
  • 你不应该把函数定义放在myframe.h,把它们全部移到myframe.cpp。头文件的声明,实现文件的定义。
  • @westernCiv 使用包括警卫。只需查找头文件和实现文件的任何示例。
  • @westernCiv:使用第 3 方框架编写 GUI 应用程序并不是开始学习 C++ 的最佳方式。在标题中包含守卫是该语言的一个非常基本的概念,在处理此类复杂主题之前必须完全理解。

标签: c++ class build compiler-errors include


【解决方案1】:

您可以将#include &lt;myframe.h&gt; 添加到“myframe.cpp”文件中。 因为“myframe.cpp”文件中没有Myframe的定义。

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多