Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
};
like image 325
thiagoh Avatar asked Dec 11 '22 20:12

thiagoh


1 Answers

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".

like image 155
Connor Hollis Avatar answered Apr 13 '23 00:04

Connor Hollis