473,804 Members | 2,154 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
10 3517

"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:36******** *************** ***********@mic rosoft.com...
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
how they interact.

Thanks

"Ben Voigt" wrote:
>>
"Max 1e6" <Ma****@discuss ions.microsoft. comwrote in message
news:AE******* *************** ************@mi crosoft.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 21 '07 #11

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

Similar topics

7
2201
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
3198
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
8698
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
3326
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
3151
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
3179
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
2893
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
5207
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
2962
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
9594
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
10343
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...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.