Error - Functions that differ only in their return types cannot be overloaded. c++

前端 未结 1 1831
傲寒
傲寒 2021-01-23 13:30

I am trying to create a library management system. I am getting a few errors which I don\'t understand. I am using Eclipse in Mac os.

My main code is:

#i         


        
相关标签:
1条回答
  • 2021-01-23 13:59
    Game *search( char gameName[]);
    DVD *search( char dvdName[]);
    Book *search( char bookName[]);
    

    the parameters for search in these 3 cases have exactly the same type. The only difference is the return type.

    You cannot overload only on return type, as which function is called is determined by parameters, not by what you assign it to.

    The easy solution is calling it searchDVD and searchGame and searchBook. At both declaration and implementation.

    The crazy solution would then involve adding this class:

    struct deferred_search {
      Library* lib;
      char const* name;
      operator DVD*()const {
        return lib->searchDVD(name);
      }
      operator Book*()const {
        return lib->searchBook(name);
      }
      operator Game*()const {
        return lib->searchGame(name);
      }
    }
    

    and in Library:

    deferred_search search( char const* name ) { return {this, name}; }
    

    which is a somewhat obscure technique to do overloading on return type. I would advise against it -- just call the functions searchTYPE.

    0 讨论(0)
提交回复
热议问题