错误-仅在返回类型上不同的函数无法重载. C ++ [英] Error - Functions that differ only in their return types cannot be overloaded. c++

查看:172
本文介绍了错误-仅在返回类型上不同的函数无法重载. C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个图书馆管理系统.我遇到了一些我不理解的错误.我在Mac OS中使用Eclipse.

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.

我的主要代码是:

#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

媒体类:

#ifndef MEDIA_H_
#define MEDIA_H_

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



#endif /* MEDIA_H_ */

游戏类:

#include "Media.h"

#ifndef GAME_H_
#define GAME_H_

class Game : public Media{
public:

    char platform[45];
};


#endif // end of "#ifndef" block

DVD类:

#include "Media.h"

#ifndef DVD_H_
#define DVD_H_

class DVD : public Media{
public:

    char director[45];
};


#endif // end of "#ifndef" block

图书班:

#include "Media.h"

#ifndef BOOK_H_
#define BOOK_H_

class Book : public Media{
public:

    char author[45];
};



#endif /* BOOK_H_ */

我得到的错误是:

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.

推荐答案

Game *search( char gameName[]);
DVD *search( char dvdName[]);
Book *search( char bookName[]);

在这3种情况下,search的参数具有完全相同的类型.唯一的区别是返回类型.

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.

简单的解决方案是将其称为searchDVDsearchGamesearchBook.在声明和实现上.

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);
  }
}

Library中的

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

这是在返回类型上进行重载的一种晦涩技术.我建议您不要使用它-只需调用函数searchTYPE.

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

这篇关于错误-仅在返回类型上不同的函数无法重载. C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆