0

我不知道如何在表单和另一个头文件之间共享数据。变量输入有问题。我不知道我如何声明它可以从表单中访问。主要计算在 other.h 中。我想在表单中设置一个变量并将其发送到 other.h,计算并返回表单。如何声明 int 输入?

注意:问题是 btnWrong

//   Form1.h 

#pragma once
    #include "other.h"


    namespace VariablesandDebugging {


        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:
            Form1(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }

        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
    //private: System::Windows::Forms::PictureBox^ pictureBox1;
        private: System::Windows::Forms::Label^  lblHeader1;
        private: System::Windows::Forms::Button^  btnRight;
        private: System::Windows::Forms::Button^  btnWrong;

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

            //My variables
        private:
            int iInches;
            int iFeet;
            bool bConvertingToInches;//???
            int iConvertedInches;
            int iRozpeti;


    #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->btnRight = (gcnew System::Windows::Forms::Button());
                this->btnWrong = (gcnew System::Windows::Forms::Button());
                // 
                // btnConvert
                // 
                this->btnRight->Location = System::Drawing::Point(60, 60);
                this->btnRight->Text = L"In Form1";
                this->btnRight->Click += gcnew System::EventHandler(this, &Form1::btnRight_Click);
                this->Controls->Add(this->btnRight);
                // 
                // btnExit
                // 
                this->btnWrong->Location = System::Drawing::Point(60, 150);
                this->btnWrong->Text = L"In other.h";
                this->btnWrong->Click += gcnew System::EventHandler(this, &Form1::btnWrong_Click);
                this->Controls->Add(this->btnWrong);


            }
    #pragma endregion
    public: System::Void btnWrong_Click(System::Object^  sender, System::EventArgs^  e) {
                getfavoritenumber2(a, b);
                MessageBox::Show(Convert::ToString(a));
                MessageBox::Show(Convert::ToString(b));
                }

    public: System::Void btnRight_Click(System::Object^  sender, System::EventArgs^  e) {
                input = 10; 
                getfavoritenumber3(a, b, output);
                MessageBox::Show(Convert::ToString(a));
                MessageBox::Show(Convert::ToString(b));
                MessageBox::Show(Convert::ToString(output));
             }
        };
    }



//other.h

#ifndef _OTHER_H_
#define _OTHER_H_

int a;
int b;
int input;  

void getfavoritenumber2(int &a, int &b)
{
    a = 3;
    b = 6;
}

void getfavoritenumber3(int &a, int &b, int &output)
{
    a = 3;
    b = 6;
    output = input*input;
}

#endif

非常感谢

4

1 回答 1

0

您可以使用文件范围静态变量。

但是这个非常古老和糟糕的想法,所以考虑一下 global / extern

于 2013-07-25T09:46:45.447 回答