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

血红的双手。 提交于 2019-12-02 07:06:35

问题


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:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include "Library.h" // include header of Library class
#include "Game.h" // include header of Game class
#include "DVD.h" // include header of DVD class
#include "Book.h" // include header of Book class
using namespace std;

void Library::insertGame( char gameName[], char platform[], int c){
    strcpy( collection[numGames].name, gameName);
    strcpy( collection[numGames].platform, platform);
    collection[numGames].copies = c;
    cout << "Game added to collection.\n";
    ++numGames;
}

void Library::deleteGame( char gameName[]){
    int i;
    for( i = 0; i < numGames; i++){
        if( strcmp( gameName, collection[i].name) == 0){
            collection[i].copies--;
            cout << "Game deleted from collection.\n";
            return;
        }
    }
    cout << "Game not found.\n";
}

void Library::insertDVD( char dvdName[], char director[], int c){
    strcpy( collection1[numDVDs].name, dvdName);
    strcpy( collection1[numDVDs].director, director);
    collection1[numDVDs].copies = c;
    cout << "DVD added to collection.\n";
    ++numDVDs;
}

void Library::deleteDVD( char dvdName[]){
    int i;
    for( i = 0; i < numDVDs; i++){
        if( strcmp( dvdName, collection1[i].name) == 0){
            collection1[i].copies--;
            cout << "DVD deleted from collection.\n";
            return;
        }
    }
    cout << "DVD not found.\n";
}

void Library::insertBook( char bookName[], char author[], int c){
    strcpy( collection2[numBooks].name, bookName);
    strcpy( collection2[numBooks].author, author);
    collection2[numBooks].copies = c;
    cout << "Book added to collection.\n";
    ++numBooks;
}

void Library::deleteBook( char bookName[]){
    int i;
    for( i = 0; i < numBooks; i++){
        if( strcmp( bookName, collection2[i].name) == 0){
            collection2[i].copies--;
            cout << "Book deleted from collection.\n";
            return;
        }
    }
    cout << "Book not found.\n";
}

Game *Library::search( char gameName[]){
    int i;
    for( i = 0; i < numGames; i++){
        if( strcmp( gameName, collection[i].name) == 0)
            return &collection[i];
    }
    return NULL;
}

DVD *Library::search( char dvdName[]){
    int i;
    for( i = 0; i < numDVDs; i++){
        if( strcmp( dvdName, collection1[i].name) == 0)
            return &collection1[i];
    }
    return NULL;
}

Book *Library::search( char bookName[]){
    int i;
    for( i = 0; i < numBooks; i++){
        if( strcmp( bookName, collection2[i].name) == 0)
            return &collection2[i];
    }
    return NULL;
}

int main(){

    Library lib;


    while( 1 ){

        char mainSelect;
        char gameOption, name[30], platform[30], copies[10];
        char dvdOption;
        char bookOption;

        // Ask the user to select an option
        cout << "\nMain Menu:"<<endl;
        cout << "D for DVDs"<<endl;
        cout << "G for Games"<<endl;
        cout << "B for Books"<<endl;
        cout << "E to exit from the system"<<endl;

        // Read user selection
        cin.getline( name, 80);
        mainSelect = name[0];

        // Switch statement to select between the options
        switch (mainSelect){
           case 'd': case 'D':
              break;
           case 'g': case 'G':
               break;
           case 'b': case 'B':
               break;
           case 'e': case 'E':
               exit(0);
               break;
        }

        if (mainSelect == 'd','D'){

           cout << "\nEnter your option:"<<endl;
           cout << "A to add a new DVD"<<endl;
           cout << "D to delete a DVD"<<endl;
           cout << "S to search for a DVD"<<endl;
           cout << "E to exit from the system"<<endl;

           cin.getline( name, 80);
           dvdOption = name[0];

        switch (dvdOption){

           case 'a': case 'A':
              cout << "Enter Name of DVD: ";
              cin.getline( name, 80);
              cout << "Enter Director of DVD: ";
              cin.getline(director, 80);
              cout << "Enter no of copies: ";
              cin.getline(copies, 80);
              lib.insertDVD( name, director, atoi(copies));
              break;
           case 'd': case 'D':
              cout << "Enter Name of DVD:\n";
              cin.getline(name, 80);
              lib.deleteDVD(name);
              break;
           case 's': case 'S':
              cout << "Enter Name of DVD:\n";
              cin.getline(name, 80);
              Game *item;
              item = lib.search( name );
              if( item != NULL){
                cout << "DVD found\n" << item->name << endl << item->platform << endl << item->copies << endl;
            }
              else
              cout << "DVD not found\n";
              break;
           case 'e': case 'E':
              exit(0);
              break;
        }
        }

        else if (mainSelect == 'g','G'){
            cout << "\nEnter your option:"<<endl;
            cout << "A to add a new game"<<endl;
            cout << "D to delete a game"<<endl;
            cout << "S to search for a game"<<endl;
            cout << "E to exit from the system"<<endl;

            cin.getline( name, 80);
            gameOption = name[0];

        switch (gameOption){

           case 'a': case 'A':
             cout << "Enter Name of Game: ";
             cin.getline( name, 80);
             cout << "Enter game platform: ";
             cin.getline(platform, 80);
             cout << "Enter no of copies: ";
             cin.getline(copies, 80);
             lib.insertGame( name, platform, atoi(copies));
             break;
           case 'd': case 'D':
             cout << "Enter Name of Game:\n";
             cin.getline(name, 80);
             lib.deleteGame(name);
             break;
           case 's': case 'S':
             cout << "Enter Name of Game:\n";
             cin.getline(name, 80);
             Game *item;
             item = lib.search( name );
             if( item != NULL){
             cout << "Game found\n" << item->name << endl << item->platform << endl << item->copies << endl;
             }
             else
             cout << "Game not found\n";
             break;
             case 'e': case 'E':
             exit(0);
             break;
        }
        }
        }
    }
    return 0;
}

I have a library class and the code for this is:

#include "DVD.h"
#include "Game.h"
#include "Book.h"

#ifndef LIBRARY_H_
#define LIBRARY_H_

class Library{
public:
    int numGames;
    int numDVDs;
    int numBooks;
    Game collection[100];
    DVD  collection1[100];
    Book collection2[100];

    Library(){
        numGames = 0;
        numDVDs = 0;
        numBooks = 0;
    }

    void insertGame( char gameName[], char platform[], int c);
    void deleteGame( char gameName[]);
    Game *search( char gameName[]);

    void insertDVD( char dvdName[], char director[], int c);
    void deleteDVD( char dvdName[]);
    DVD *search( char dvdName[]);

    void insertBook( char bookName[], char director[], int c);
    void deleteBook( char bookName[]);
    Book *search( char bookName[]);
};

#endif // end of "#ifndef" block

Media class:

#ifndef MEDIA_H_
#define MEDIA_H_

class Media{
public:
    int copies;
    char name[45];
};



#endif /* MEDIA_H_ */

Game class:

#include "Media.h"

#ifndef GAME_H_
#define GAME_H_

class Game : public Media{
public:

    char platform[45];
};


#endif // end of "#ifndef" block

DVD class:

#include "Media.h"

#ifndef DVD_H_
#define DVD_H_

class DVD : public Media{
public:

    char director[45];
};


#endif // end of "#ifndef" block

Book class:

#include "Media.h"

#ifndef BOOK_H_
#define BOOK_H_

class Book : public Media{
public:

    char author[45];
};



#endif /* BOOK_H_ */

And the errors I get are:

1. Member declaration not found.
2. return type out-of-line definition of 'library::search'differs from that in the declaration.
3. Functions that differ only in their return types cannot be overloaded.

回答1:


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.



来源:https://stackoverflow.com/questions/29167762/error-functions-that-differ-only-in-their-return-types-cannot-be-overloaded-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!