I have been trying to do forward declaration to allow access between classes. I have read here that:
- I can’t include the “a.h” file when forward declaring A in b.h
I haven’t been able to find very much about namespaces during forward declaration. And I’m fairly certain I am messing this up (I just don’t know where to put them). The errors I get are after the relevant code snippets.
This is what I did:
-
I have split my class definitions from all being in .h to .cpp and .h
-
I have #ifndef guards in my .h files
These are my files:
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(){....}
Errors for Form1.cpp It’s the same thing for every definition
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 – no errors here
#include "stdafx.h"
#include "OpenGL.h"
#include "Form1.h"
OpenGLForm::COpenGL::COpenGL(){};
... other functions that go the same way
GUI_1.cpp – the main function
#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;
}
I am going to keep trying to decypher these messages, but I’d appreciate any help meanwhile.
In OpenGL.h, you need to forward-declare Form1 in the correct namespace:
namespace GUI_1 {
ref class Form1;
}
And forward-declare COpenGL in Form1.h the same way:
namespace OpenGLForm {
ref class COpenGL;
}
Important: Make sure these declareations are outside of other namespace blocks, and remove your existing forward-declarations from inside the classes.
In Form1.cpp, it would be clearer to define the member functions within a namespace block:
namespace GUI_1 {
Form1::Form1(void)
…
}
The two .cpp files include Form1.h and OpenGL.h in different orders. It would be better to only include Form1.h, and have Form1.h include OpenGL.h.