473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class instances in C++

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::MyMeth od(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++?

Apr 20 '07 #1
10 3516

"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:1B******** *************** ***********@mic rosoft.com...
>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::MyMeth od(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.MyMeth od(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
Apr 20 '07 #2
OK, so now I have (omitting windows designer code):
#pragma once
namespace WA1 {

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ e) {
MyClass Instance;
double x = 2.0;
double F = Instance.MyMeth od(&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







"Ben Voigt" wrote:
>
"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:1B******** *************** ***********@mic rosoft.com...
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::MyMeth od(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.MyMeth od(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
Apr 20 '07 #3
Max 1e6 wrote:
OK, so now I have (omitting windows designer code):
#pragma once
namespace WA1 {

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ e) {
MyClass Instance;
double x = 2.0;
double F = Instance.MyMeth od(&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_sentine l
#define myclass_sentine l

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

#endif

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

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

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

-cd
Apr 20 '07 #4

"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:AE******** *************** ***********@mic rosoft.com...
OK, so now I have (omitting windows designer code):
#pragma once
namespace WA1 {

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ 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.MyMeth od(&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







"Ben Voigt" wrote:
>>
"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:1B******* *************** ************@mi crosoft.com...
>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::MyMeth od(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.MyMeth od(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

Apr 20 '07 #5
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.

"Carl Daniel [VC++ MVP]" wrote:
Max 1e6 wrote:
OK, so now I have (omitting windows designer code):
#pragma once
namespace WA1 {

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ e) {
MyClass Instance;
double x = 2.0;
double F = Instance.MyMeth od(&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_sentine l
#define myclass_sentine l

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

#endif

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

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

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

-cd
Apr 20 '07 #6
On Apr 20, 1:14 pm, Max 1e6 <Max...@discuss ions.microsoft. comwrote:
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.

"Carl Daniel [VC++ MVP]" wrote:
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::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ e) {
MyClass Instance;
double x = 2.0;
double F = Instance.MyMeth od(&x);
}
};
}

Apr 20 '07 #7
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

"Ben Voigt" wrote:
>
"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:AE******** *************** ***********@mic rosoft.com...
OK, so now I have (omitting windows designer code):
#pragma once
namespace WA1 {

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
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(S ystem::Object^ sender,
System::EventAr gs^ 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.MyMeth od(&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







"Ben Voigt" wrote:
>
"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:1B******** *************** ***********@mic rosoft.com...
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::MyMeth od(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.MyMeth od(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


Apr 20 '07 #8
I think I have a work around. I placed MyClass in a class library and now I
have:
using namespace MyClassClassLib rary; 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?

Apr 20 '07 #9

"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:CC******** *************** ***********@mic rosoft.com...
>I think I have a work around. I placed MyClass in a class library and now I
have:
using namespace MyClassClassLib rary; 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.
Apr 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2200
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for x in range(10):
7
1716
by: Carlos Ribeiro | last post by:
I'm looking for ways to load new class definitions at runtime (I'm not talking about object instances here, so a persistent object database isn't what I am looking for ). I'm aware of a few options, but I would like to discuss the possible side effects for a potentially big, long running application server. I am developing a business application based on workflow concepts. The model is quite simple: a business process is modelled as a...
6
3196
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
166
8683
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
3324
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do it from the instance.
4
3149
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
5
3176
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object.
8
2889
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
21
5202
by: Nikolaus Rath | last post by:
Hello, Can someone explain to me the difference between a type and a class? After reading http://www.cafepy.com/article/python_types_and_objects/ it seems to me that classes and types are actually the same thing: - both are instances of a metaclass, and the same metaclass ('type') can instantiate both classes and types. - both can be instantiated and yield an "ordinary" object - I can even inherit from a type and get a class
44
2957
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.