Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Return nothing from a function having return type

Return nothing from a function having return type

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meerokh
    wrote on last edited by
    #1

    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++

    L J J 3 Replies Last reply
    0
    • M meerokh

      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++

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      U 1 Reply Last reply
      0
      • M meerokh

        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++

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        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
        }

        1 Reply Last reply
        0
        • M meerokh

          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++

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Lost User

            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.

            U Offline
            U Offline
            User 13896304
            wrote on last edited by
            #5

            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?

            L 1 Reply Last reply
            0
            • U User 13896304

              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?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I don't understand what you are saying. If the return type of your getPackage function is a Package 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 returning nullptr to indicate failure, then the function must return a pointer to a Package 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;
              

              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups