0

I'm trying to make it so that when I click a button on my Visual C++ project, it opens another form, kinda acting like a java JOptionPane.showInputDialog, but making it look like the way I want. I'm trying to open it with Form21^form2=gcnew Form21(); form2->ShowDialog(); but all it says is

1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'Form21' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(253): error C2061: syntax error : identifier 'Form21'
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2065: 'form2' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\lesson 2\lesson 2\Form1.h(254): error C2227: left of '->ShowDialog' must point to class/struct/union/generic type
1>          type is ''unknown-type''

Form 2 should be all fine, but here is the code for it anyways

#pragma once

namespace Lesson2 {

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

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form2()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::TextBox^  textBox2;
    protected: 

    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->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(12, 9);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(61, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Username :";
            this->label1->Click += gcnew System::EventHandler(this, &Form2::label1_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(81, 8);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(108, 20);
            this->textBox1->TabIndex = 1;
            this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form2::textBox1_TextChanged);
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(12, 54);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(59, 13);
            this->label2->TabIndex = 2;
            this->label2->Text = L"Password: ";
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(81, 47);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(108, 20);
            this->textBox2->TabIndex = 3;
            // 
            // Form2
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(211, 88);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            this->DoubleBuffered = true;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->MaximizeBox = false;
            this->MinimizeBox = false;
            this->Name = L"Form2";
            this->Text = L"Create an account";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
             }
};
}

I know its says its lesson two, but that's just because I was teaching myself some stuff. I pick up this kinda stuff real easy. Except when this happens. Anyone see whats going wrong?

4

1 回答 1

1

Form21 很可能在另一个文件中声明,因此编译器找不到 Form21 类型。

在 Form1.h 的顶部,添加#include "Form21.h"(或包含 Form21 的类声明的头文件的名称,如果它不同)。

我还建议您阅读有关头文件和 cpp 文件(头文件中似乎有代码)、前向声明和其他一些关于 c++/cli 的基本内容。

编辑:发布Form2代码后,错误是该行Form21^form2=gcnew Form21();应该是Form2^form2=gcnew Form2();. 你声明Form2,不是Form21

祝你好运!

于 2012-08-09T16:05:58.483 回答