C++/CLI转发声明问题



我一直在尝试进行前向声明,以允许类之间的访问。我在这里读到:

  • 在b.h中转发声明a时,我不能包含"a.h"文件

在前向声明过程中,我一直找不到太多关于名称空间的信息。我很确定我把这件事搞砸了(我只是不知道该把它们放在哪里)。我得到的错误是在相关的代码片段之后。

这就是我所做的:

  • 我已经将类定义从.h中的所有定义拆分为.cpp和.h

  • 我在.h文件中有#ifndef防护程序

这些是我的文件:

Form1.cpp

#include "stdafx.h"
#include "OpenGL.h"
#include "Form1.h"
#include "serialcom.h"
#include "calculations.h"
using namespace GUI_1;
GUI_1::Form1::Form1(void)
    {....
    }
void GUI_1::Form1::chlabel2(float num)
    {....
    }
int GUI_1::Form1::updateHand(int source){....}
void GUI_1::Form1::resetHand(){....}

Form1.cpp的错误每个定义都是一样的

error C2872: 'GUI_1' : ambiguous symbol
could be 'GUI_1'
or       OpenGLForm::GUI_1'

Form1.h

#ifndef form1
#define form1
using namespace OpenGLForm; 
//error C2871: 'OpenGLForm' : a namespace with this name does not exist
ref class COpenGL;
namespace GUI_1 {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        OpenGLForm::COpenGL^ o_gl;
            // error C2653: 'OpenGLForm' : is not a class or namespace name
        Form1(void);
        void chlabel2(float num);
    protected:
        ...
...};}

OpenGL.h

#ifndef opengl
#define opengl
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
// Declare globals
....
namespace OpenGLForm 
{
    using namespace System::Windows::Forms;
    using namespace GUI_1;
    // error C2871: 'GUI_1' : a namespace with this name does not exist
    ref class GUI_1::Form1;
    // error C2653: 'GUI_1' : is not a class or namespace name
    // 'Form1' uses undefined class 'OpenGLForm::GUI_1'
    public ref class COpenGL: public System::Windows::Forms::NativeWindow
    {
    public:
        Form1^ form1;
            // error C2059: syntax error : ';'
            // error C2238: unexpected token(s) preceding ';'
            // error C2143: syntax error : missing ';' before '^'
        ...
};
}
#endif

OpenGL.cpp-此处无错误

#include "stdafx.h"
#include "OpenGL.h"
#include "Form1.h"
OpenGLForm::COpenGL::COpenGL(){};
... other functions that go the same way

GUI_1.cpp-主要功能

#include <vcclr.h>
#include <stdio.h>
#include <stdlib.h>
#include "Form1.h"
#include "calculations.h"
#include "serialcom.h"
#include "OpenGL.h"
using namespace GUI_1;
using namespace OpenGLForm;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    GUI_1::Form1 ^form1 = gcnew GUI_1::Form1();
    // error C2059: syntax error : '='
    OpenGLForm::COpenGL ^open_gl = gcnew OpenGLForm::COpenGL();
    form1->o_gl = open_gl;
    // error C2143: syntax error : missing ';' before '->'
    open_gl->form1 = form1;
    // error C2059: syntax error : '='
    return 0;
}

我将继续尝试解密这些信息,但在此期间,如果有任何帮助,我将不胜感激。

  • 在OpenGL.h中,您需要在正确的命名空间中转发声明Form1:

    命名空间GUI_1{ref类Form1;}

  • 并以相同的方式在Form1.h中转发声明COpenGL:

    命名空间OpenGLForm{参考类别COpenGL;}

重要提示:请确保这些声明位于其他命名空间块之外,并从类内部删除现有的正向声明。

  • 在Form1.cpp中,在名称空间块中定义成员函数会更清楚:

    命名空间GUI_1{表格1::表格1(无效)。。。}

  • 这两个.cpp文件包括Form1.h和OpenGL.h,它们的顺序不同。最好只包括Form1.h,并且让Form1.h包括OpenGL.h。

最新更新