class instances in C++

G

Guest

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++?
 
B

Ben Voigt

Max 1e6 said:
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++?

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
 
G

Guest

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
 
C

Carl Daniel [VC++ MVP]

Max said:
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

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
 
B

Ben Voigt

Max 1e6 said:
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;

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.
 
G

Guest

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.
 
J

John

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);
}
};
}
 
G

Guest

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
 
G

Guest

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?
 
B

Ben Voigt

Max 1e6 said:
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?

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.
 
B

Ben Voigt

Max 1e6 said:
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).
 

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