returning NULL but getting error C2440: 'return' : cannot convert from 'int' to 'const &'

StackOverflow https://stackoverflow.com/questions/13356176

  •  28-11-2021
  •  | 
  •  

سؤال

I've got a method that follows

class BuildOrderStrategy
{

public:

    virtual const Urgency& getUrgency() = 0;
    ...
}

which implementation follows

const Urgency& RandomBuildOrderStrategy::getUrgency()
{
    return NULL;
}

but at compile time I'm getting this error

error C2440: 'return' : cannot convert from 'int' to 'const Urgency &'

at this time I really do want to return a NULL value from getUrgency method.. so.. what is the problem with my code? how can I fix it? I came from java world where this is completely possible..

the code of Urgency is this

class Urgency : public Investment
{
private:

public:
    Urgency(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3);
    ~Urgency(void);
};
هل كانت مفيدة؟

المحلول

Returning NULL from this type of function doesn't make a lot of sense. You probably want a pointer not a reference.

NULL is defined as 0 ( or even (void*)0 ) and since 0 is an integer that is what you are returning in this function which doesn't work with a reference ( which has to reference an actual object )

In this case returning a pointer to a Urgency object in your function makes more sense. So try changing the reference to a pointer.

const Urgency* RandomBuildOrderStrategy::getUrgency()
{
    return NULL;
}

Or even try returning an Urgency type that has been initialized in a special way that allowed you to check later if it is "empty".

نصائح أخرى

You are confusing references and pointers. If you return a reference, it cannot be NULL, because a reference never can. If you want to return a pointer, you need to return Urgency*, not Urgency&.

In C++ unlike Java and others, you can't have a null reference. Null pointers yes. But the concept of a null reference doesn't exist.

Remember that NULL is really just 0. Now stop and think for a second, does it make sense to set a RandomBuildOrderStrategy to 0?

You can use a pointer as suggested by Pukku.

Returning NULL would be fine if your function returned a pointer. Of course, it doesn't, so it's not fine.

References are guaranteed to refer to something valid in a well behaved program (i.e., no UB). NULL would not convert to a valid reference even if there existing a conversion from int (0, NULL) to const Urgency&.

The returning of a reference assures that the function doesn't return NULL but always returns a reference to an object. If you want to check for NULL you should return a pointer. OTOH if you want to return a reference, return an empty Urgency object.

You are trying to return a reference to NULL which is not allowed in C++

A reference is a name that acts as an alias, or an alternative name, for a previously defined variable.

you could change your getUrgency interface to return pointer instead. As your Urgency is inherited from Investment, you could return Investment* pointer

class BuildOrderStrategy
{

public:
    virtual const Investment* getUrgency() = 0;
    ...
}

const Urgency* RandomBuildOrderStrategy::getUrgency()
{
    return NULL;   
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top