Return nothing from a function having return type
-
I have a class PackageSingleton and got a function getPackage whose return type is another class Package. This function iterates over a queue and retrieves a package and executes it. If a queue is empty i want to return nothing and keep looping the queue until a package is found. In C++ NULL cant be returned and returning a nullptr is giving an error
no viable conversion from returned value of type 'nullptr_t' to function return type 'Package
. Any idea how should i handle this situation.
c++
-
I have a class PackageSingleton and got a function getPackage whose return type is another class Package. This function iterates over a queue and retrieves a package and executes it. If a queue is empty i want to return nothing and keep looping the queue until a package is found. In C++ NULL cant be returned and returning a nullptr is giving an error
no viable conversion from returned value of type 'nullptr_t' to function return type 'Package
. Any idea how should i handle this situation.
c++
You can only return a nullptr if the defined return type is a pointer to something. If the return type is some sort of object then you must return an actual object of that type. You could solve this by changing the function to return a pointer to the relevant Package object.
-
I have a class PackageSingleton and got a function getPackage whose return type is another class Package. This function iterates over a queue and retrieves a package and executes it. If a queue is empty i want to return nothing and keep looping the queue until a package is found. In C++ NULL cant be returned and returning a nullptr is giving an error
no viable conversion from returned value of type 'nullptr_t' to function return type 'Package
. Any idea how should i handle this situation.
c++
The common solution is to return an empty object. But this requires that the class supports such (usually by the default constructor without arguments creating such an empty object and providing a member function that checks for the object being empty). An example is the
std::string
class:std::string somefunc()
{
// An empty string
std::string str();// Optionally set the string here // Might return an empty string return str; // May also use //return std::string();
}
std::string str = somefunc();
if (!str.empty())
{
// String is not empty
} -
I have a class PackageSingleton and got a function getPackage whose return type is another class Package. This function iterates over a queue and retrieves a package and executes it. If a queue is empty i want to return nothing and keep looping the queue until a package is found. In C++ NULL cant be returned and returning a nullptr is giving an error
no viable conversion from returned value of type 'nullptr_t' to function return type 'Package
. Any idea how should i handle this situation.
c++
If you are using C++17, there is std::optional designed just for this situation. You could return a raw pointer (assuming the collection outlives the use of its objects), a unique_ptr (construct a new object) or shared_ptr (and store the Packages as shared_ptr objects). Another option is to pass a non-const reference to a Package and have the method return a bool indicating success or failure. As has been pointed out, you could also return an empty object. This is the only choice if your design is constrained to return just an instance.
-
You can only return a nullptr if the defined return type is a pointer to something. If the return type is some sort of object then you must return an actual object of that type. You could solve this by changing the function to return a pointer to the relevant Package object.
If we didn't define the return type a pointer then how we will get nullptr. I have so far used char, int return type from Bitdefender Error 1020 what is the relevant package for returning pointer type?
-
If we didn't define the return type a pointer then how we will get nullptr. I have so far used char, int return type from Bitdefender Error 1020 what is the relevant package for returning pointer type?
I don't understand what you are saying. If the return type of your
getPackage
function is aPackage
object, then that is what you must return. If the function fails then you must find some other way to signal that failure, perhaps by throwing an exception. If you want the possibility of returningnullptr
to indicate failure, then the function must return a pointer to aPackage
object, thus:Package* getPackage()
{
Package* pPackage;// code to create a new package ... pPackage = new Package(); // ... if (some problem with creating the object) pPackage = nullptr; return pPackage;
}