2013-05-04 181 views
0

在globaldata.h中,我將typedef結構定義爲GlobalData。在我的主要cpp程序中,我包含globaldata.h,然後聲明
GlobalData globalData;

在聲明之後,我包含了createNetDialog.h和gui.h.爲什麼我只能在gui.h中引用globalData而不是createNetDialog.h?在預編譯期間,兩個包含文件都應該粘貼到我的.cpp文件中,所以globalData應該在右側可見?無法訪問全局變量

我已經定義了一個全球國際結構這裏:
globaldata.h

typedef struct { 
    string netFilename; 
    DataFileReader *dataFileReader; 
} GlobalData; 

在我的C++代碼,我有:
gui.cpp

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include "DataFileReader.h" 
#include "globaldata.h" 

GlobalData globalData; 

#include "createNetDialog.h" 
#include "gui.h" 


using namespace System; 
using namespace System::Windows::Forms; 
using namespace std; 

[STAThread] 
void Main(array<String^>^ args) 
{ 
    Application::EnableVisualStyles(); 
    Application::SetCompatibleTextRenderingDefault(false); 

    Carnac::gui formMain; 
    Application::Run(%formMain); 
} 


createNetDialog.h(在createNetDialog構造函數中)

#pragma once 


namespace Carnac { 

    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 createNetDialog 
    /// </summary> 
    public ref class createNetDialog : public System::Windows::Forms::Form 
    { 
    public: 
     createNetDialog(void) 
     { 
      InitializeComponent(); 

      // left of '.size' must have a class/struct/union 
      // left of '.dataFileReader' must have a class/struct/union 
      // 'globalData':undeclared identifier despite being declared right before 
      // this file is included in the .cpp file 
      for (unsigned int i=0; i<globalData.dataFileReader->_headerItems.size(); i++) 
      { 
          // do something 
      } 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~createNetDialog() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    protected: 

    protected: 

    private: System::Windows::Forms::Button^ buttonCreate; 
    private: System::Windows::Forms::Button^ buttonCreateCancel; 




    private: 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     void InitializeComponent(void) 
     { 
      this->buttonCreate = (gcnew System::Windows::Forms::Button()); 
      this->buttonCreateCancel = (gcnew System::Windows::Forms::Button()); 
      this->SuspendLayout(); 
      // 
      // buttonCreate 
      // 
      this->buttonCreate->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)); 
      this->buttonCreate->Location = System::Drawing::Point(16, 288); 
      this->buttonCreate->Name = L"buttonCreate"; 
      this->buttonCreate->Size = System::Drawing::Size(75, 23); 
      this->buttonCreate->TabIndex = 7; 
      this->buttonCreate->Text = L"Create"; 
      this->buttonCreate->UseVisualStyleBackColor = true; 
      // 
      // buttonCreateCancel 
      // 
      this->buttonCreateCancel->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right)); 
      this->buttonCreateCancel->Location = System::Drawing::Point(404, 288); 
      this->buttonCreateCancel->Name = L"buttonCreateCancel"; 
      this->buttonCreateCancel->Size = System::Drawing::Size(75, 23); 
      this->buttonCreateCancel->TabIndex = 8; 
      this->buttonCreateCancel->Text = L"Cancel"; 
      this->buttonCreateCancel->UseVisualStyleBackColor = true; 
      this->buttonCreateCancel->Click += gcnew System::EventHandler(this, &createNetDialog::buttonCreateCancel_Click); 
      // 
      // createNetDialog 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(491, 323); 
      this->Controls->Add(this->buttonCreateCancel); 
      this->Controls->Add(this->buttonCreate); 
      this->Name = L"createDialog"; 
      this->Text = L"CreateDialog"; 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 
#pragma endregion 
    private: System::Void buttonCreateCancel_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
       this->Close(); 
    } 
}; 
} 


gui.h(末)

#pragma once 

#include <msclr\marshal_cppstd.h> 

namespace Carnac { 

    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 gui 
    /// </summary> 
    public ref class gui : public System::Windows::Forms::Form 
    { 
     public: 
     gui(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

     protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~gui() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
     protected: 

     private: int _dataColumnSelected; 
     private: System::Windows::Forms::Button^ buttonLoad; 

     private: 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      this->buttonLoad = (gcnew System::Windows::Forms::Button()); 
      this->SuspendLayout(); 
      // 
      // buttonLoad 
      // 
      this->buttonLoad->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)); 
      this->buttonLoad->Text = L"Load Data"; 
      this->buttonLoad->UseVisualStyleBackColor = true; 
      this->buttonLoad->Click += gcnew System::EventHandler(this, &gui::buttonLoad_Click); 
      this->buttonLoad->Location = System::Drawing::Point(67, 66); 
      this->buttonLoad->Name = L"buttonLoad"; 
      this->buttonLoad->Size = System::Drawing::Size(75, 23); 
      this->buttonLoad->TabIndex = 0; 
      this->buttonLoad->UseVisualStyleBackColor = true; 
      // 
      // gui 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->BackColor = System::Drawing::SystemColors::AppWorkspace; 
      this->ClientSize = System::Drawing::Size(234, 171); 
      this->Controls->Add(this->buttonLoad); 
      this->Name = L"gui"; 
      this->Text = L"Carnac"; 
      this->ResumeLayout(false); 

     } 
#pragma endregion 
     static std::string toStandardString(System::String^ string) 
     { 
      using System::Runtime::InteropServices::Marshal; 
      System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string); 
      char* charPointer = reinterpret_cast<char*>(pointer.ToPointer()); 
      std::string returnString(charPointer, string->Length); 
      Marshal::FreeHGlobal(pointer); 
      return returnString; 
     } 

     private: System::Void buttonPredictRun_Click(System::Object^ sender, System::EventArgs^ e) 
     { 
     } 
     private: System::Void buttonTestLoad_Click(System::Object^ sender, System::EventArgs^ e) 
     { 
     } 
     private: System::Void buttonLoad_Click(System::Object^ sender, System::EventArgs^ e) 
     { 
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; 

      openFileDialog1->Filter = "Data Files|*.dat"; 

      if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
      { 
       ifstream inFile; 

       String^ strfilename = openFileDialog1->InitialDirectory + openFileDialog1->FileName; 

       // THIS WORKS 
       // Why can I use globalData here but not in createNetDialog.h? 
       globalData.dataFileReader = new DataFileReader(toStandardString(strfilename)); 
      } 
     } 
    }; 
} 

感謝所有幫助

回答

0

是的,從GUI編譯時,全球國際會議是在兩個gui.h和createNetDialog.h,可見。 cpp。在globalData.h中,您所定義的全部是類型,而不是該類型的全局變量。當從任何其他cpp文件編譯時,gui.h和createNetDialog.h都不能在gui.cpp中看到全局變量的聲明。

下面是你想要做的:在globalData.h中聲明類型和一個extern全局變量。包含你想使用它的每個文件的globalData.h,不要依賴於其他文件在你想要使用它的文件之前包含它。在只有一個 cpp文件(當前爲gui.cpp),declare實際的全局變量。

// globalData.h 
#include "DataFileReader.h" 
typedef struct { 
    string netFilename; 
    DataFileReader *dataFileReader; 
} GlobalData; 

extern GlobalData globalData; 

// EVERY file where you use globalData 
#include "globalData.h" 

// gui.cpp 
#include "globaldata.h" 

GlobalData globalData; 
+0

我試過你的解決方案,但我得到了同樣的結果。我包含globaldata.h的唯一.cpp文件是gui.cpp。但是,是的,如果我將它包含在其他.cpp文件中,你的方式會更好,並且我會爲未來的增長保留它。但現在,它只包含在gui.cpp文件中。 – user2350086 2013-05-04 17:00:11

+0

我的歉意。有一個visual studio創建的createNetDialog.cpp文件,所以我沒有包含那個globaldata.h。謝謝您的幫助! – user2350086 2013-05-04 17:19:07