×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Classes and Where Do I put stuff in MFC

Classes and Where Do I put stuff in MFC

Classes and Where Do I put stuff in MFC

(OP)
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

RE: Classes and Where Do I put stuff in MFC

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.

RE: Classes and Where Do I put stuff in MFC

(OP)
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

RE: Classes and Where Do I put stuff in MFC

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.

RE: Classes and Where Do I put stuff in MFC

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close