2011-10-16 48 views
0

我一直在尋找解決這個問題的方法,現在已經有相當長一段時間了。我有一個標準的Form1.h,其中聲明瞭一些全局變量。我想從單獨的.cpp文件中的函數訪問此表單的屬性。因此,這裏是我如何努力做到這一點:從函數訪問表單的屬性

//Form1.h 

#pragma once 

#include "stdafx.h" 
#include "test.h" 


unsigned char vMAC1; 
unsigned char vMAC2; 
unsigned char vMAC3; 
unsigned char vMAC4; 
unsigned char vMAC5; 
unsigned char vMAC6; 


extern long pNum; 

//ARP Variables 

unsigned char gMAC1; 
unsigned char gMAC2; 
unsigned char gMAC3; 
unsigned char gMAC4; 
unsigned char gMAC5; 
unsigned char gMAC6; 

extern unsigned char mMAC1; 
extern unsigned char mMAC2; 
extern unsigned char mMAC3; 
extern unsigned char mMAC4; 
extern unsigned char mMAC5; 
extern unsigned char mMAC6; 

namespace Artemis_v { 
    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    using namespace System::Runtime::InteropServices; 
    using namespace System::Threading; 
    using System::IntPtr; 
    /// <summary> 
    /// Summary for Form1 
    /// </summary> 

    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

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

    protected: 

    ///// And so on just standard compiler-created statements.. 

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
     ChangeText(this); 
    } 


// test.h 

#ifndef TEST_H 
#define TEST_H 

namespace Artemis_v 
{ 
    ref class Form1; 
    void ChangeText(Form1 ^frm); 
} 

#endif 


// test.cpp 

#include "StdAfx.h" 
#include "test.h" 
#include "Form1.h" 

namespace Artemis_v 
{ 
    void ChangeFormText(Form1 ^frm) 
    { 
     frm->Text="Hello!"; 
    } 
} 

此代碼給我LNK2005已經定義的錯誤,我知道這是因爲被我重新聲明變量時,包括我Form1.h在TEST.CPP。我可以找到解決此問題的任何解決方法,或者我應該刪除變量嗎?

回答

1

你的一個.cpp文件應該聲明變量沒有的「外部」和你的.h文件應與「外部」

+0

謝謝,現在的作品宣佈他們,雖然,你知道什麼更好的辦法讓窗體的句柄,而不將其作爲參數傳遞給函數? – astralmaster