2

クラス間のアクセスを許可するために前方宣言をしようとしています。私はここでそれを読んだ:

  • bh で A を前方宣言するときに「ah」ファイルを含めることができません

前方宣言中に名前空間についてあまり見つけることができませんでした。そして、私はこれを台無しにしていると確信しています(どこに置くべきかわかりません)。私が得るエラーは、関連するコードスニペットの後にあります。

これは私がしたことです:

  • クラス定義を .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;
}

私はこれらのメッセージを解読しようとし続けますが、それまでの間、何か助けていただければ幸いです.

4

1 に答える 1

7
  • OpenGL.h では、正しい名前空間で Form1 を前方宣言する必要があります。

    名前空間 GUI_1 { ref クラス Form1; }

  • Form1.h で COpenGL を同じ方法で前方宣言します。

    名前空間 OpenGLForm { ref クラス COpenGL; }

重要: これらの宣言が他の名前空間ブロックの外側にあることを確認し、クラス内から既存の前方宣言を削除してください。

  • Form1.cpp では、名前空間ブロック内でメンバー関数を定義する方が明確です。

    名前空間 GUI_1 { Form1::Form1(void) ... }

  • 2 つの .cpp ファイルには、Form1.h と OpenGL.h が異なる順序で含まれています。Form1.h のみをインクルードし、Form1.h に OpenGL.h をインクルードすることをお勧めします。

于 2012-07-18T20:32:28.673 に答える