0

I know there is a lot of posts about this topic, but still I can't get it to work. I created a new project that called "nis".

This is "nis.cpp":

// nis.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace nis;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}

I have two forms. This is "Form1.h":

#ifndef FORM1_H
#define FORM1_H
#pragma once
#include "Form2.h"
namespace nis {

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::Button^  button1;
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->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(78, 77);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(144, 53);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(282, 253);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
        this->ResumeLayout(false);

    }
#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             this->Hide();
             Form2^ f1 = gcnew Form2();
             f1->ShowDialog();
         }
};
}

#endif

And this is "Form2.h":

#ifndef FORM2_H
#define FORM2_H
#pragma once
#include "Form1.h"
namespace nis {

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::Button^  button1;
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->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(44, 102);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(149, 53);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        this->button1->UseVisualStyleBackColor = true;
        // 
        // Form2
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(282, 253);
        this->Controls->Add(this->button1);
        this->Name = L"Form2";
        this->Text = L"Form2";
        this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
        this->ResumeLayout(false);

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

Can you please tell me what's wrong with the inclusion guards? I get this errors:

Error   1   error C2065: 'Form2' : undeclared identifier
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error   2   error C2065: 'f1' : undeclared identifier   
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error   3   error C2061: syntax error : identifier 'Form2'  
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error   4   error C2065: 'f1' : undeclared identifier   
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 86
Error   5   error C2227: left of '->ShowDialog' must point to 
class/struct/union/generic type d:\users\lior\documents\visual studio 
2010\projects\nis\nis\Form1.h   86

Thank you so much!

1 Answers1

0

You include form 1 in form 2 and form 2 in form 1. What do you expect the preprocessor to do? You'll have to make a choice here! In general, modules should form a hierarchy rather than a network, and certainly not a graph with cycles, which would mean that a module in the end includes itself.

If you want to mutually refer between form 1 and form 2, don't put all your code in headers but make separate implementation files. Your headers will hold only your class decls. The accompanying C++ files will hold the code of the methods, including their refs to the other form.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
  • OK I know that, but can you tell me what the solution is? I want the option to navigate from "Form1" to "Form2" and reverse. I thought that the using of "#ifndef" should solve the recursion. – user1074988 Apr 02 '16 at 09:16
  • Hi. I am sorry but C++ is new to me, I just don't know how to get this solving and apply it into my forms. Could you please help me? – user1074988 Apr 02 '16 at 17:57
  • The following example shows you how to separate header and implementation: http://p2p.wrox.com/c-programming/92954-c-class-example-separate-header-implementation-file.html By the way, to be honest, C++ is not the easiest way to work with Microsoft .net (which is what you're doing). C# is more mainstream for that area, and IronPython is even easier. Having said that, learning C++ is a very good and durable investment, since you can do al lot more with it then programming for MS .net. – Jacques de Hooge Apr 02 '16 at 18:41
  • I am really trying yo apply it on my forms, but I am not so c++ knowledgeable and I don't know how to treat with some code rows that comes automatically with the form creation. could someone just told me how my files suppost to look like? thanks a lot. – user1074988 Apr 10 '16 at 16:09