C++ error: Invalid use of incomplete type …

后端 未结 3 1654
余生分开走
余生分开走 2021-01-05 05:26

I have a small- to medium-size project that I am doing for my software engineering course this semester. I have chosen to do it in C++ (gtkmm). I am doing okay so far but I

相关标签:
3条回答
  • 2021-01-05 05:50

    To fix the error you should replace #include "Login_Dialog.h" with forward declaration of Login_Dialog class in Main_Window.h. Then include Login_Dialog.h in Main_Window.cpp and Main_Window.h in Login_Dialog.cpp. BTW, the same can be done for many other files/classes.

    0 讨论(0)
  • 2021-01-05 06:01

    When I try and do this I get the error presented at the very top of this post. If I try and remove the class MainWindow; and replace it with #include "MainWindow.h" I run into circular reference issues with headers.

    But this is the issue. You need to move the implementation into a separate implementation (.cpp) file. You can use forward declarations to break circular references in header files, but you have to have both headers available before you attempt to use your type.

    You have to include the full definition of you class before you can use it -- not just a forward declaration. Forward declarations are only useful to other forward declarations -- the compiler needs to know what type it's working with before it can generate code.

    0 讨论(0)
  • 2021-01-05 06:07

    You can get away with forward declaring MainWindow in Login_Dialog.h as long as you only forward declar a pointer to the type (which you do), and you add this to the top of Login_Dialog.h so the compiler knows to expect to see a class declaration at some later time.

    class MainWindow;
    

    Then in Login_Dialog.cpp, include "mainwindow.h" like this.

    /*
     * Login_Dialog.cpp
     *
     *  Created on: Mar 2, 2010
     *      Author: Matthew
     */
    
    #include "Login_Dialog.h"
    #include "MainWindow.h"
    

    That should do it.

    0 讨论(0)
提交回复
热议问题