Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes and Where Do I put stuff in MFC

Status
Not open for further replies.

asimov500

Programmer
Mar 14, 2008
21
I am not new to programming but I am new to Visual C++ and the MFC.

I read the example I get the cli versions to work in dos but the problems come when I start to use MFC.

I have never used Classes before and so I read the example and I almost get it to work but not quite in the form.

I know a Class has to be defined before main and so I did, in Cat Class.cpp I have this code:-

// Cat class.cpp : main project file.

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

using namespace Catclass;

class Cat
{
public:
int age;
int weight;
};

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

// Create the main window and run it
MessageBox::Show ("Age="+String::Concat(Frisky.age),"test");
Application::Run(gcnew Form1());
return 0;
}

Voila a MessageBox comes up with Frisky's age, which is 5. No problems there.
But then when I want to access data in the form it's as if Frisky.age does not exist. It won't even compile when I use the same bit of code in Form1.h

-snip-
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

MessageBox::Show (String::Concat(Frisky.age),"test");
}
};
-snip-


I cannot understand why this line works in main but not in Form.h. I am having trouble with MFC. Any help would be appreciated. I declared it as Public: so it should be accessable by all parts of the program surely.

Asimov
 
Asimov,

I can't really help you except to point out that the above example is not MFC. They're written in a Microsoft specific language called C++/CLI, an proprietory extension of C++, and use the .NET framework (i.e. forms) instead of MFC. I don't know .NET stuff yet.

If you want to look at MFC you should generate a basic application using the Visual C++ new application wizard (File->New->Project->MFC->MFC Application). In that case the language used will be C++, not C++/CLI.
 
Hi,

I am actually using Visual C++ 2008 Express edition.
The only way I could get it to use forms was to select
New/Project/CLR/Windows Forms Application.

There is no option for MFC. From what I read I assume that this version of C++ used MFC so I may have put two and two together and got six.

I presumed that .net actually used MFC as standard.

Anyway apart from all that I don't know why my code wouldn't work.
Thanks anyway.

Asimov
 
MFC isn't included in the express edition. MFC predates .NET and doesn't rely on the .NET framework. .NET doesn't use MFC though there are ways to make them work together. .NET uses a newer user interface development library called Windows Forms (or something like that) which runs on top of the .NET framework which is installed automatically with newer versions of Windows (it's optional for older versions of Windows). You should generally think of MFC and .NET development as two different things. Visual C++ 2008 Express edition only targets .NET development.

Now regarding your problem. The form needs to know what the class Cat is before it can be used. You've defined Cat in your "Cat class.cpp" file but the form in "Form1.h" can't see that. You need to place the definition of Cat in a header file, say "Cat.h", and then include that header file in "Form1.h". Your actual instance of Cat which you've called Frisky also needs to be made known to the form, possibly by passing it as a parameter to the form. Dealing with header files is one of the first hurdles in learning C++, or C++/CLI.

Windows Forms can be programmed in any language which supports the .NET framework. If you're new to C++ then you may also want to consider using C# instead. It's generally considered to be easier than C++ to learn though it's specific to .NET and Microsoft.

 
Hi CooperDBM,

Thankyou for your help. Using the #include file method worked.
I now know how to include a class in my program. Don't know everything about classes yet, but I hope to learn more.

I have done a lot of programming in the past, but the big problem I have with C++/CLI is the syntax of things and things like classes which I have never come across before.

Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top