Microsoft make brain bleed

J

jeff.sharkfinn

I'm trying to create a pointer to a class in form1.h button1 event
handler. It should be simple... but no. This example is as simple as I
can make:

Form1.h (produced with the designer containing a single button)
***********************************************************************************
#pragma once


namespace MyProject {

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
///
/// WARNING: If you change the name of this class, you will need to
change the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </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;
// private: MyClass^ MyPtr;
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(91, 65);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(104, 32);
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(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 256);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);

}
#pragma endregion
#include "MyClass.h"
#pragma once
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
// MyClass^ MyPtr = gcnew MyClass();
// MyPtr->MyMethod();
MyClass instance;
instance.MyMethod();
}
};
}

************************
At the end here, I want to have the event handler for button 1 execute
MyMethod();

MyClass.h Produced by adding a C++ class
*****************************
#pragma once

ref class MyClass
{
public:
MyClass(void);
static void MyMethod(void);
};
***********************************************
MyClass.cpp

#include "StdAfx.h"
#include "MyClass.h"
#include "Form1.h"

using namespace MyProject;

MyClass::MyClass(void)
{
}
void MyClass::MyMethod()
{
}
***************************************************
MyProject.cpp

// MyProject.cpp : main project file.

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

using namespace MyProject;

[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 don't want to have to put all my executable code in Form1.h
I want to have separate files for unrelated classes and call them from
various event handlers.
The way the files are as shown I get LNK2020 unresolved token.
If I pull the #include "MyClass.h" from Form1.h I get error C2065
undeclared identifier.

I'm fit to be tied. I've spent hours searching for an answer and it
seems to be a common problem world-wide with no resolution. I've tried
all the old fashioned ways too. MyClass* pMyClass = new MyClass();
etc.

Anybody able to help?
TIA
 
C

Carl Daniel [VC++ MVP]

I don't want to have to put all my executable code in Form1.h
I want to have separate files for unrelated classes and call them from
various event handlers.
The way the files are as shown I get LNK2020 unresolved token.

Which symbol is undefined? Presumably you have both .cpp files in your
project.
If I pull the #include "MyClass.h" from Form1.h I get error C2065
undeclared identifier.

Yes, you definitely need the declaration of MyClass to be visible in order
to instantiate it and call a member function on it.
I'm fit to be tied. I've spent hours searching for an answer and it
seems to be a common problem world-wide with no resolution. I've tried
all the old fashioned ways too. MyClass* pMyClass = new MyClass();
etc.

If you make MyClass a non-CLR class (i.e. class MyClass instead of public
ref class MyClass), then "the old fashioned way" will work just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top