Mew*_*ewa 2 c++ c++-cli forward-declaration winforms
我一直在尝试进行前向声明以允许类之间的访问.我在这里读到:
在前向声明期间,我无法找到很多名称空间.我相当肯定我搞砸了(我只是不知道把它们放在哪里).我得到的错误是在相关代码片段之后.
这就是我做的:
我已将我的类定义从.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(){....}
Run Code Online (Sandbox Code Playgroud)
Form1.cpp的错误每个定义都是一样的
error C2872: 'GUI_1' : ambiguous symbol
could be 'GUI_1'
or OpenGLForm::GUI_1'
Run Code Online (Sandbox Code Playgroud)
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:
...
...};}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
OpenGL.cpp - 这里没有错误
#include "stdafx.h"
#include "OpenGL.h"
#include "Form1.h"
OpenGLForm::COpenGL::COpenGL(){};
... other functions that go the same way
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
我将继续尝试对这些消息进行decypher,但我同时感谢任何帮助.
在OpenGL.h中,您需要在正确的命名空间中正向声明Form1:
命名空间GUI_1 {ref class Form1; }
并以相同的方式在Form1.h中转发声明COpenGL:
命名空间OpenGLForm {ref class COpenGL; }
要点:确保这些声明不在其他命名空间块之内,并从类中删除现有的前向声明.
在Form1.cpp中,在命名空间块中定义成员函数会更清楚:
命名空间GUI_1 {Form1 :: Form1(void)...}
这两个.cpp文件包含不同顺序的Form1.h和OpenGL.h.最好只包含Form1.h,并让Form1.h包含OpenGL.h.