Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2007

Tip: Looking for answers? Try searching our database.

class instances in C++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Max 1e6 - 20 Apr 2007 01:20 GMT
I have an application form named Form1.h. The code for this form is a
namespace (MyNamespace) with two classes in it (Form1 and MyClass). Form1 has
windows designer code and some button event handlers. In those event handlers
I want to reference methods in the MyClass class. I use syntax like this:  
double F = MyClass::MyMethod(x,y);

As soon as I type the second colon I get a full list of all the exposed
methods in MyClass and I sense I going the right way. But, I debug and get
two errors:

1) MyClass is not a class or namespace name
2) MyMethod identifier not found

I am not sure why this happens but I sense its because I need an instance of
MyClass to work with in the class Form1. In VB I create an instance of a
class with the new keywork e.g. MyInstance=new MyClass.  How is this done in
C++?
Ben Voigt - 20 Apr 2007 01:52 GMT
>I have an application form named Form1.h. The code for this form is a
> namespace (MyNamespace) with two classes in it (Form1 and MyClass). Form1
[quoted text clipped - 17 lines]
> in
> C++?

Does MyClass need to maintain state, in such a way that it would be worth
keeping track of instances?  If no, put the 'static' modifier on your
functions, then your function calls will work as-is.

If yes, then you can do:

MyClass instance;
double F = instance.MyMethod(x, y);

which establishes an instance that is local to the current scope.  If the
object needs to live longer, we need to know if it is a ref class or a
native C++ class.  For native C++:
MyClass* pInstance = new MyClass();
double F = pInstance->MyMethod(x, y);
... later
delete pInstance;

For .NET ref classes:
MyClass^ pInstance = gcnew MyClass();
double F = pInstance->MyMethod(x, y);
... no need to delete, garbage collector does the work for you
Max 1e6 - 20 Apr 2007 15:22 GMT
OK, so now I have (omitting windows designer code):
#pragma once

namespace WA1 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    public ref class Form1 : public System::Windows::Forms::Form
    {
    private: System::Void button1_Click(System::Object^  sender,
System::EventArgs^  e) {
                MyClass Instance;
                double x = 2.0;
                double F = Instance.MyMethod(&x);
            }
    };

    public class MyClass{
        double MyMethod(double *x){
            return *x*2;
        }
    };
}

Again, intellisense seems to recognize MyClass and MyMethod bcause as soon
as I type double F = Instance.,   MyMethod shows up as a choice. But on degug
I get:

error C2065: 'MyClass' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'Instance'
error C2065: 'Instance' : undeclared identifier
error C2228: left of '.MyMethod' must have class/struct/union

> >I have an application form named Form1.h. The code for this form is a
> > namespace (MyNamespace) with two classes in it (Form1 and MyClass). Form1
[quoted text clipped - 39 lines]
> double F = pInstance->MyMethod(x, y);
> .... no need to delete, garbage collector does the work for you
Carl Daniel [VC++ MVP] - 20 Apr 2007 15:48 GMT
> OK, so now I have (omitting windows designer code):
> #pragma once
[quoted text clipped - 32 lines]
> error C2065: 'Instance' : undeclared identifier
> error C2228: left of '.MyMethod' must have class/struct/union

Most likely, you need to #include the definition of MyClass.  Typically this
would be done by placing

#include "MyClass.h"

somewhere near the top of this file.  Intellisense "sees" into all of the
files in your project, but the compiler itself will only allow you to
reference definitions that you've explicitly #included into the current
compiland (the .cpp file).  This is in stark contrast to how C#, J#, VB, and
Java all work - in these languages, the compiler "sees" every file in your
project in a single compilation, so you don't need to explicitly expose
definitions in one file to referencing code in another file.

You're working on Windows forms code, which puts the entire class defintion
into the header file.   This is not normal C++ style (but rather is a
by-product of the fact that the designers were written by the C# team to
generate C# code).  If you've followed this style in your own code (e.g.
MyClass), then you have to watch out for "ODR Violations".  In simple terms,
if a class is defined entirely in a header file, you are only guaranteed to
end up with a working program when that header file is #included into a
single compiland (.cpp file) exactly one time.

For your own classes, you should follow normal C++ coding style and NOT put
the entire class definition in the header file.

// MyClass.h
#ifndef myclass_sentinel
#define myclass_sentinel

namespace MyNamespace
{
   class MyClass
   {
       public:
           void MyMethod1();
           int MyMethod2();
           ...
   };
}

#endif

//  MyClass.cpp
#include "MyClass.h"

namespace MyNamespace
{
   void MyClass::MyMethod1()
   {
       //.. actual function body
   }

   int MyClass::MyMethod2()
   {
       //... actual function body
   }
}

-cd
Max 1e6 - 20 Apr 2007 19:14 GMT
Carl
I was able to put everything together for MyClass using the procedure you
detailed. However, I am still not clear on what step(s) I must now take to
have access to that class in the code for Form1.h.

> > OK, so now I have (omitting windows designer code):
> > #pragma once
[quoted text clipped - 92 lines]
>
> -cd
John - 20 Apr 2007 21:34 GMT
> Carl
> I was able to put everything together for MyClass using the procedure you
> detailed. However, I am still not clear on what step(s) I must now take to
> have access to that class in the code for Form1.h.

This seems to work just fine (since I've tried). I don't understand
what the deal is! Note MyMethod must be a public method in order to
the form ti use.

#pragma once
using namespace System;

namespace WA1 {
       using namespace System;
       using namespace System::ComponentModel;
       using namespace System::Collections;
       using namespace System::Windows::Forms;
       using namespace System::Data;
       using namespace System::Drawing;

       public class MyClass{
       public:
           double MyMethod(double *x){
                       return *x*2;
               }
       };

       public ref class Form1 : public System::Windows::Forms::Form
       {
       private: System::Void button1_Click(System::Object^  sender,
System::EventArgs^  e) {
                               MyClass Instance;
                                double x = 2.0;
                               double F = Instance.MyMethod(&x);
                        }
       };
}
Ben Voigt - 20 Apr 2007 18:35 GMT
> OK, so now I have (omitting windows designer code):
> #pragma once
[quoted text clipped - 12 lines]
> System::EventArgs^  e) {
> MyClass Instance;

The compiler doesn't know MyClass is a type name, because it hasn't seen
MyClass yet.  Move the definition of MyClass ahead of where you use it.  If
you have two mutually interacting classes, this will require that you use a
forward declaration of each class "class MyClass;" to make it usable as a
parameter and/or return type, and then both class bodies with forward
declarations of member functions, and then bodies of each member function.
That way everything is available to the compiler before it's used.

> double x = 2.0;
> double F = Instance.MyMethod(&x);
[quoted text clipped - 67 lines]
>> double F = pInstance->MyMethod(x, y);
>> .... no need to delete, garbage collector does the work for you
Max 1e6 - 20 Apr 2007 22:42 GMT
Yup, putting MyClass first works. However, my form designer dissappears.

I get the feeling I just don't understand enough about the project files and
how they interact.

Thanks

> > OK, so now I have (omitting windows designer code):
> > #pragma once
[quoted text clipped - 92 lines]
> >> double F = pInstance->MyMethod(x, y);
> >> .... no need to delete, garbage collector does the work for you
Ben Voigt - 21 Apr 2007 00:53 GMT
> Yup, putting MyClass first works. However, my form designer dissappears.

Yes, because the form designer doesn't actually understand C++, it just
looks for particular patterns in the file.  If you move a block of code into
a different file and make it visible in the first file using #include, the
compiler will see it, but the form designer won't get confused (as much).

> I get the feeling I just don't understand enough about the project files
> and
[quoted text clipped - 107 lines]
>> >> double F = pInstance->MyMethod(x, y);
>> >> .... no need to delete, garbage collector does the work for you
Max 1e6 - 20 Apr 2007 23:00 GMT
I think I have a work around. I placed MyClass in a class library and now I
have:
using namespace MyClassClassLibrary; at the top of WA1.  I can see the forms
designer and the button works.

Are there any cases where this approach isn't effective?
Ben Voigt - 21 Apr 2007 00:51 GMT
>I think I have a work around. I placed MyClass in a class library and now I
> have:
[quoted text clipped - 3 lines]
>
> Are there any cases where this approach isn't effective?

If two classes have to mutually use each other, then they have to be in the
same assembly, so your fix wouldn't work.  You could have used #include
"MyClass.h" instead of a separate assembly and a reference.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.