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

I have a method that follows

class BuildOrderStrategy { public: virtual const Urgency& getUrgency() = 0; ... } 

implementation of which follows

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

but at compile time I get this error

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

at this time, I really want to return NULL from the getUrgency method. so .. what is the problem with my code? how can i fix this? I came from the Java world where possible.

Urgency code is

 class Urgency : public Investment { private: public: Urgency(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3); ~Urgency(void); }; 
+4
source share
6 answers

Returning NULL from this type of function does not make much sense. You probably want the pointer not to be a link.

NULL is defined as 0 (or even (void *) 0), and since 0 is the integer that you return in this function that does not work with the link (which should refer to the actual object)

In this case, returning the pointer to the urgency object in your function makes more sense. So try changing the link to the pointer.

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

Or even try to return the type of urgency that was initialized in a special way, which allowed you to check later if it is "empty".

+3
source

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

+3
source

In C ++, unlike Java and others, you cannot have a null reference. Zero pointers yes. But the concept of null reference does not exist.

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

You can use the pointer suggested by Pukku.

+3
source

Returning NULL would be great if your function returns a pointer. Of course, this is not so, so this is not normal.

Links usually refer to something that acts in a well-executed program (i.e. there is no UB). NULL will not be converted to a valid reference, even if there is a conversion from int (0, NULL) to const Urgency& .

+2
source

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

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

you can change the getUrgency interface to return a pointer instead. Since your Urgency is inherited from an investment, you can return the Investment * pointer

 class BuildOrderStrategy { public: virtual const Investment* getUrgency() = 0; ... } const Urgency* RandomBuildOrderStrategy::getUrgency() { return NULL; } 
+1
source

Returning a link ensures that the function does not return NULL, but always returns a reference to the object. If you want to check for NULL, you must return a pointer. OTOH, if you want to return the link, return an empty Urgency object.

+1
source

Source: https://habr.com/ru/post/1445694/


All Articles