检查类是否为NULL的问题 [英] Problem with checking if class is NULL

查看:64
本文介绍了检查类是否为NULL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


好​​的,我正在当地一所社区大学学习C ++课程,而且我在一个小项目上工作了b
为了我自己的学习需要,我遇到了一个

问题。


我正在构建的类包含指向

同一个类,这样我就可以遍历对象,使用对象

Next_Object()成员函数返回下一个

对象的地址列表。


在我的循环中,我设置了一个指向类的指针,指向Next_Object()

返回,我需要检查是否为NULL为了退出循环

(即tmp_object!= NULL)。这是我的问题所在。编译器

表示我需要重载!=运算符来执行此操作,但如果

对象为NULL,它将如何调用它的成员函数?


任何帮助都将不胜感激。

- TwinkiE_HunteR-G再次罢工!

tw ************** @ hard-wire.net
:HARD-WiRE网页设计

http: //www.hard-wire.net

Hello all,

Ok, I am taking a C++ class at a local community college, and I''m
working on a small program for my own learning needs and I ran into a
problem.

The class I''m building includes a pointer to another object of the
same class, so that I can loop through the objects, using the objects
Next_Object() member function which returns the adress of the next
object in the list.

In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it''s member function?

Any help would be appreciated.
- TwinkiE_HunteR-G strikes again!
: tw**************@hard-wire.net
: HARD-WiRE Web Design
: http://www.hard-wire.net

推荐答案

2004年4月8日星期四03:11:02 GMT,TwinkiE_HunteR-G

< tw ******************** @ hard-wire.net>在comp.lang.c ++中写道:
On Thu, 08 Apr 2004 03:11:02 GMT, TwinkiE_HunteR-G
<tw********************@hard-wire.net> wrote in comp.lang.c++:
大家好,

好的,我正在当地社区大学读一堂C ++课,我是
为了我自己的学习需要开发一个小程序,我遇到了一个
问题。

我正在构建的类包含指向另一个对象的指针。 />同一个类,这样我就可以遍历对象,使用对象
Next_Object()成员函数返回列表中下一个
对象的地址。

在我的循环中,我设置了一个指向Next_Object()
返回的类的指针,我需要检查是否为NULL以便退出循环
(即tmp_object!= NULL)。这是我的问题所在。编译器
说我需要重载!=运算符来执行此操作,但如果
对象为NULL,它将如何调用它的成员函数?

任何帮助将不胜感激。
Hello all,

Ok, I am taking a C++ class at a local community college, and I''m
working on a small program for my own learning needs and I ran into a
problem.

The class I''m building includes a pointer to another object of the
same class, so that I can loop through the objects, using the objects
Next_Object() member function which returns the adress of the next
object in the list.

In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it''s member function?

Any help would be appreciated.




您发布的描述和

代码之间必须有所区别您没有。任何指向任何对象类型的指针,甚至是指向函数的
指针都可以与NULL进行比较。如果您的编译器确实不接受这个,那么您要比较的不是指针。


发布显示问题的最小代码示例,

数据类型的定义。


-

Jack Klein

主页:< a rel =nofollowhref =http://JK-Technology.Comtarget =_ blank> http://JK-Technology.Com


comp.lang.c http: //www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp。 lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



There must be a difference between the description that you posted and
the code that you didn''t. Any pointer to any object type, and even
pointers to functions can be compared to NULL. If your compiler does
not accept this, what you are trying to compare is NOT a pointer.

Post the smallest possible code sample that show problem, with the
definitions of the data types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


4月8日星期四2004 03:11:02 GMT,TwinkiE_HunteR-G

& LT; TW ******************** @ hard-wire.net>在comp.lang.c ++中写道:
On Thu, 08 Apr 2004 03:11:02 GMT, TwinkiE_HunteR-G
<tw********************@hard-wire.net> wrote in comp.lang.c++:
大家好,

好的,我正在当地社区大学读一堂C ++课,我是
为了我自己的学习需要开发一个小程序,我遇到了一个
问题。

我正在构建的类包含指向另一个对象的指针。 />同一个类,这样我就可以遍历对象,使用对象
Next_Object()成员函数返回列表中下一个
对象的地址。

在我的循环中,我设置了一个指向Next_Object()
返回的类的指针,我需要检查是否为NULL以便退出循环
(即tmp_object!= NULL)。这是我的问题所在。编译器
说我需要重载!=运算符来执行此操作,但如果
对象为NULL,它将如何调用它的成员函数?

任何帮助将不胜感激。
Hello all,

Ok, I am taking a C++ class at a local community college, and I''m
working on a small program for my own learning needs and I ran into a
problem.

The class I''m building includes a pointer to another object of the
same class, so that I can loop through the objects, using the objects
Next_Object() member function which returns the adress of the next
object in the list.

In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it''s member function?

Any help would be appreciated.




您发布的描述和

代码之间必须有所区别您没有。任何指向任何对象类型的指针,甚至是指向函数的
指针都可以与NULL进行比较。如果您的编译器确实不接受这个,那么您要比较的不是指针。


发布显示问题的最小代码示例,

数据类型的定义。


-

Jack Klein

主页:< a rel =nofollowhref =http://JK-Technology.Comtarget =_ blank> http://JK-Technology.Com


comp.lang.c http: //www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp。 lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



There must be a difference between the description that you posted and
the code that you didn''t. Any pointer to any object type, and even
pointers to functions can be compared to NULL. If your compiler does
not accept this, what you are trying to compare is NOT a pointer.

Post the smallest possible code sample that show problem, with the
definitions of the data types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


TwinkiE_HunteR-G写道:
TwinkiE_HunteR-G wrote:
在我的循环中,我设置了一个指向Next_Object()
返回的类的指针,我需要检查它是否为NULL以便退出循环
(即tmp_object!= NULL)。这是我的问题所在。编译器
说我需要重载!=运算符来执行此操作,但如果
对象为NULL,它将如何调用它的成员函数?

任何帮助将不胜感激。
In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it''s member function?

Any help would be appreciated.




你确定tmp_object是指针吗?



Are you sure tmp_object is a pointer?


这篇关于检查类是否为NULL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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