2012-07-18 68 views
2

我一直在尝试做前向声明以允许类之间的访问。我在这里,上面写着:C++/CLI转发声明问题

  • 我不能包括“A.H”文件时向前声明中b.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; 
} 

我会继续努力,以正是DeCypher这些消息,但我感谢任何帮助。

回答

7
  • 在OpenGL.h,你需要在正确的命名空间前瞻性声明Form1中:

    命名空间GUI_1 { 裁判Form1类; }

  • 和前瞻性声明COpenGL在Form1.h以同样的方式:

    命名空间OpenGLForm { 引用类COpenGL; }

要点:确保这些declareations其他命名空间块之外,并从类中删除现有的前瞻性声明。

  • 在Form1.cpp,这将是更清晰的一个命名空间块中定义的成员函数:

    命名空间GUI_1 { Form1中:: Form1中(空隙) ... }

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

+0

谢谢!这摆脱了大多数错误。我现在得到的大多数错误都链接到这一行:'Form1^f_one';在OpenGL.h中。 错误仍然相同:'错误C2143:语法错误:缺少';' '^'之前。我无法在网上找到很多有关这方面的信息:/ – Mewa 2012-07-18 20:42:31

+0

如果答案是有帮助的,你可能应该给它一个upvote :) – 2012-07-18 20:45:26

+0

我不能:(我需要更多的声望,我很抱歉认为! – Mewa 2012-07-18 20:52:53