Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 访问表单';函数的属性_.net_Visual Studio_C++ Cli - Fatal编程技术网

.net 访问表单';函数的属性

.net 访问表单';函数的属性,.net,visual-studio,c++-cli,.net,Visual Studio,C++ Cli,我一直在寻找解决这个问题的方法已经有一段时间了。我有一个标准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 ch

我一直在寻找解决这个问题的方法已经有一段时间了。我有一个标准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!";
    }
}
//Form1.h
#布拉格语一次
#包括“stdafx.h”
#包括“test.h”
无符号字符vMAC1;
无符号字符vMAC2;
无符号字符vMAC3;
无符号字符vMAC4;
无符号字符vMAC5;
无符号字符vMAC6;
外长pNum;
//ARP变量
无符号字符gMAC1;
无符号字符gMAC2;
无符号字符gMAC3;
无符号字符gMAC4;
无符号字符gMAC5;
无符号字符gMAC6;
外部无符号字符mMAC1;
外部无符号字符mMAC2;
外部无符号字符mMAC3;
外部无符号字符mMAC4;
外部无符号字符mMAC5;
外部无符号字符mMAC6;
名称空间Artemis_v{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
使用名称空间System::Runtime::InteropServices;
使用名称空间系统::线程;
使用System::IntPtr;
/// 
///表格1摘要
/// 
public ref类Form1:public System::Windows::Forms::Form
{
公众:
表格1(无效)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~Form1()
{
if(组件)
{
删除组件;
}
}
受保护的:
/////以此类推,只是标准的编译器创建的语句。。
私有:系统::无效按钮1\u单击(系统::对象^sender,系统::事件参数^e){
更改文本(本);
}
//测试h
#ifndef试验
#定义测试
名称空间Artemis_v
{
参考类别表格1;
无效更改文本(格式1^frm);
}
#恩迪夫
//test.cpp
#包括“StdAfx.h”
#包括“test.h”
#包括“表格1.h”
名称空间Artemis_v
{
void ChangeFormText(Form1^frm)
{
frm->Text=“你好!”;
}
}

这段代码给了我LNK2005已经定义的错误,我知道这是因为当我在test.cpp中包含Form1.h时,我的变量被重新声明。我能找到解决这个问题的方法吗?或者我应该删除这些变量吗?

你的一个.cpp文件应该声明没有“extern”的变量,而你的.h文件应该声明有“extern”的变量

谢谢,现在它可以工作了,不过,您知道有没有更好的方法来获取表单句柄而不将其作为参数传递给函数?