如何在C ++中返回NULL对象 [英] How to return NULL object in C++

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

问题描述

我知道这可能是一个重复的: C ++返回NULL对象,如果没有找到搜索结果

I know that this might be a duplicate of: C++ return a "NULL" object if search result not found

但是我的代码有一些不同的地方,因为星号不能解决我的问题,这是: / p>

BUT, there's something different going on with my code because the asterisk doesn't solve my problem, which is this:

Normal Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return NULL;
   }
   //other stuff
   return Normal(something, somethingElse);
}

但是我得到一个错误引用 / code> line:从'int'转换为非标量类型'正常'请求

But I get an error referencing the return NULL line: conversion from ‘int’ to non-scalar type ‘Normal’ requested

另一个错误和警告引用了最后一个返回行:警告:取地址临时从正常*转换为非标量类型'正常请求

And another error and warning that referencing the last return line: warning: taking address of temporary and conversion from ‘Normal*’ to non-scalar type 'Normal' requested

我理解为什么我收到此警告,但我不知道如何解决它。如何在函数结束后在最后一行返回一个 Normal 对象,如何返回 NULL 对象第一次? (如果有这些类型的退货的条款,请让我知道,所以我也可以读更多。)

I understand why I am getting this warning, but I don't know how to fix it. How do I return a Normal object in the last line that persists after the function ends and how do I return a NULL object that first time? (If there's a term for these types of returns, please let me know so I can also read up on it more.)

为了澄清评论者的问题,我尝试这些东西:

To clarify a commenter's question, I've tried these things:

我试着这样做: Normal * Sphere :: hit(Ray ray)文件和正常*命中(Ray ray); 在头文件中,我得到这个错误: (Ray)'在类'Sphere'中不匹配

I tried doing this: Normal *Sphere::hit(Ray ray) in the cpp file and Normal *hit( Ray ray ); in the header file and I get this error: error: prototype for ‘Normal* Sphere::hit(Ray)’ does not match any in class 'Sphere'

我也尝试过: *在头文件中的cpp文件和正常*命中(Ray ray); 命中(Ray ray)第二个return语句:无法将返回中的Normal *转换为Normal Sphere :: *

I also tried this: Normal Sphere::*hit(Ray ray) in the cpp file and Normal *hit( Ray ray); in the header file and I get this error for the second return statement: cannot convert 'Normal*' to 'Normal Sphere::*' in return

进一步说明:我不是问指针是如何工作的。 (这不是主要问题。)我想知道关于C ++中的指针的语法。所以,给定我上面指定的函数,我收集了我应该指定一个返回一个指针,因为C ++没有空对象。得到它了。但是,问题就变成了:函数原型应该是什么样子?在cpp文件中,我有Bala建议(这是我原来,但更改,因为以下错误):

Further clarification: I'm not asking about how pointers work. (That wasn't the main question.) I'm wondering about syntax regarding pointers in C++. So, given the function I've specified above, I've gleaned that I should specify a return a pointer because C++ doesn't have null objects. Got it. BUT, the problem then becomes: what should the function prototype look like? In the cpp file, I have what Bala suggested (which is what I had originally but changed it because of the following error):

Normal* Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return NULL;
   }
   //other stuff
   return new Normal(something, somethingElse);
}

在头文件中,我有 Normal * hit (Ray ray),但我仍然得到这个消息:'Normal * Sphere :: hit(Ray)'的原型与类'Sphere' c $ c>在这一点上,我不清楚为什么它找不到该函数原型。这是头文件:

In the header file, I have Normal *hit(Ray ray), but I still get this message: prototype for 'Normal* Sphere::hit(Ray)' does not match any in class 'Sphere' At this point, it is unclear to me why it can't find that function prototype. Here is the header file:

class Sphere
{
    public:
        Sphere();
        Vector3 center;
        float radius;
        Normal* hit(Ray ray);
};

任何人都能看到为什么抱怨没有匹配的原型 Sphere 类中命中? (我可能会将此移动到一个单独的问题...)

Can anyone see why it's complaining that there doesn't exist a matching prototype for hit in the Sphere class? (I might move this to a separate question...)

推荐答案

我认为你需要像

Normal* Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return NULL;
   }
   //other stuff
   return new Normal(something, somethingElse);
}

才能返回NULL;

这篇关于如何在C ++中返回NULL对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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