C ++中的类实例 [英] class instances in C++

查看:72
本文介绍了C ++中的类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Form1.h的申请表。此表单的代码是一个

命名空间(MyNamespace),其中包含两个类(Form1和MyClass)。 Form1有

windows设计器代码和一些按钮事件处理程序。在那些事件处理程序中

我想引用MyClass类中的方法。我使用这样的语法:

double F = MyClass :: MyMethod(x,y);


我输入第二个冒号后,我得到一个MyClass中所有暴露的

方法的完整列表,我感觉我走的是正确的方式。但是,我调试并获得

两个错误:


1)MyClass不是类或命名空间名称

2)MyMethod找不到标识符


我不知道为什么会发生这种情况但我感觉到它因为我需要一个

MyClass的实例来在Form1类中工作。在VB中,我使用新的关键字创建了一个

类的实例,例如: MyInstance =新的MyClass。怎么在

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

推荐答案



"最大1e6 < Ma **** @ discussion.microsoft.com写信息

新闻:1B ************************ ********** @ microsof t.com ...

"Max 1e6" <Ma****@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.com...

>我有一个名为Form1.h的申请表。此表单的代码是一个

命名空间(MyNamespace),其中包含两个类(Form1和MyClass)。 Form1



windows设计器代码和一些按钮事件处理程序。在那些事件中

处理程序

我想引用MyClass类中的方法。我使用这样的语法:

double F = MyClass :: MyMethod(x,y);


我输入第二个冒号后,我得到一个MyClass中所有暴露的

方法的完整列表,我感觉我走的是正确的方式。但是,我调试并获得

两个错误:


1)MyClass不是类或命名空间名称

2)MyMethod找不到标识符


我不知道为什么会这样,但我感觉到它因为我需要一个实例



MyClass to在Form1类中工作。在VB中,我使用新的关键字创建了一个

类的实例,例如: MyInstance =新的MyClass。怎么做?

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



MyClass是否需要维护状态,以便值得跟踪实例?
跟踪实例?如果不是,请将''static''修饰符放在

函数上,然后你的函数调用将按原样运行。


如果是,那么你可以这样做:


MyClass实例;

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


它建立了一个当前范围的本地实例。如果

对象需要更长时间,我们需要知道它是ref类还是

本机C ++类。对于原生C ++:

MyClass * pInstance = new MyClass();

double F = pInstance-> MyMethod(x,y);

....稍后

删除pInstance;


对于.NET ref类:

MyClass ^ pInstance = gcnew MyClass( );

double F = pInstance-> MyMethod(x,y);

....无需删除,垃圾收集器为你工作

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


好的,现在我有(省略windows设计器代码):

#pragma一次

命名空间WA1 {


使用命名空间系统;

使用命名空间System :: ComponentModel;

使用命名空间System :: Collections;

使用命名空间System :: Windows :: Forms;

使用命名空间System :: Data;

使用命名空间System :: Drawing;

public ref class Form1:public System :: Windows :: Forms :: Form

{

private:System :: Void button1_Click(System :: Object ^ sender,

System :: EventArgs ^ e){

MyClass实例;

double x = 2.0;

double F = Instance.MyMethod(& x);

}

};


公共类MyClass {

double MyMethod(double * x){

return * x * 2 ;

}

};

}


再次,intellisense似乎认识到MyClass和MyMethod因为我输入双F =实例,因此很快就会出现

。,MyMethod显示为一个选择。但是在degug上

我得到:


错误C2065:''MyClass'':未声明的标识符

错误C2146:语法错误:缺少'';''在标识符之前''实例''

错误C2065:''实例'':未声明的标识符

错误C2228:''。MyMethod的左边''必须有班级/结构/工会





" Ben Voigt"写道:
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







"Ben Voigt" wrote:

>

" Max 1e6" < Ma **** @ discussion.microsoft.com写信息

新闻:1B ************************ ********** @ microsof t.com ...
>
"Max 1e6" <Ma****@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.com...

我有一个名为Form1.h的申请表。此表单的代码是一个

命名空间(MyNamespace),其中包含两个类(Form1和MyClass)。 Form1



windows设计器代码和一些按钮事件处理程序。在那些事件中

处理程序

我想引用MyClass类中的方法。我使用这样的语法:

double F = MyClass :: MyMethod(x,y);


我输入第二个冒号后,我得到一个MyClass中所有暴露的

方法的完整列表,我感觉我走的是正确的方式。但是,我调试并获得

两个错误:


1)MyClass不是类或命名空间名称

2)MyMethod找不到标识符


我不知道为什么会这样,但我感觉到它因为我需要一个实例



MyClass to在Form1类中工作。在VB中,我使用新的关键字创建了一个

类的实例,例如: MyInstance =新的MyClass。怎么做?

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



MyClass是否需要维护状态,以便值得跟踪实例?
跟踪实例?如果不是,请将''static''修饰符放在

函数上,然后你的函数调用将按原样运行。


如果是,那么你可以这样做:


MyClass实例;

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


它建立了一个当前范围的本地实例。如果

对象需要更长时间,我们需要知道它是ref类还是

本机C ++类。对于原生C ++:

MyClass * pInstance = new MyClass();

double F = pInstance-> MyMethod(x,y);

....稍后

删除pInstance;


对于.NET ref类:

MyClass ^ pInstance = gcnew MyClass( );

double F = pInstance-> MyMethod(x,y);

....无需删除,垃圾收集器为你工作


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写道:
Max 1e6 wrote:

好​​的,现在我有了(省略了windows设计器代码):

#pragma一次


命名空间WA1 {


使用命名空间系统;

using namespace System :: ComponentModel;

using namespace System :: Collections;

using namespace System :: Windows :: Forms;

using namespace System :: Data;

使用命名空间System :: Drawing;

public ref class Form1:public System :: Windows :: Forms :: Form

{

private:System :: Void但是ton1_Click(System :: Object ^ sender,

System :: EventArgs ^ e){

MyClass Instance;

double x = 2.0;

double F = Instance.MyMethod(& x);

}

};


公共类MyClass {

double MyMethod(double * x){

return * x * 2;

}

};

}


再次,当我输入双倍F =时,intellisense似乎认识到MyClass和MyMethod为

实例。,MyMethod显示为选择。

但是在degug上我得到:


错误C2065:''MyClass'':未声明的标识符

错误C2146:语法错误:在标识符''实例'之前缺少'';'''

错误C2065:''实例'':未声明的标识符

错误C2228:''。MyMethod''的左边必须有class / struct / union
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



最有可能的是,你需要#include MyClass的定义。通常这个

将通过放置


#include" MyClass.h"


靠近这个文件的顶部。智能感知看到进入你项目中的所有

文件,但编译器本身只允许你将你明确#include的参考定义引入当前的

compiland(.cpp文件)。这与C#,J#,VB和/或
Java的工作方式形成鲜明对比 - 在这些语言中,编译器看到了单个编译中

项目中的每个文件,因此您不需要在一个文件中明确公开

定义来引用另一个文件中的代码。


您正在使用Windows表单代码,它将整个类定义

放入头文件中。这不是正常的C ++风格(而是由C#团队编写的设计人员生成C#代码的事实的副产品)。如果你在自己的代码中遵循这种风格(例如

MyClass),那么你必须注意ODR违规。简单来说,如果一个类完全是在头文件中定义的话,那么
只保证当这个头文件被#included成为一个工作程序时,你只能得到
一个

单个compiland(.cpp文件)恰好一次。


对于你自己的类,你应该遵循正常的C ++编码风格而不是放在

头文件中的整个类定义。


// MyClass.h

#ifndef myclass_sentinel

#define myclass_sentinel


命名空间MyNamespace

{

class MyClass

{

public:

void MyMethod1();

int MyMethod2();

...

};

}


#endif


// MyClass.cpp

#include" MyClass.h"


命名空间MyNamespace

{

void MyClass :: MyMethod1()

{

// ..实际功能体

}


int MyClass :: MyMet hod2()

{

// ...实际功能体

}

}


-cd

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


这篇关于C ++中的类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆