错误 - 仅返回类型不同的函数不能重载。由 小码哥发布于

2024-05-02

我正在尝试创建一个图书馆管理系统。我收到一些我不明白的错误。我在 Mac 操作系统中使用 Eclipse。

我的主要代码是:

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

我有一个库类,其代码是:

#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[]);

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

您不能仅在返回类型上重载,因为调用哪个函数是由参数决定的,而不是由您分配给它的内容决定的。

最简单的解决方案是调用它searchDVD and searchGame and searchBook。无论是声明还是执行。

疯狂的解决方案将涉及添加此类:

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

这是一种对返回类型进行重载的有点晦涩的技术。我建议不要这样做——只需调用函数即可searchTYPE.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误 - 仅返回类型不同的函数不能重载。由 小码哥发布于 的相关文章

  • 当从后台工作程序发生事件时,XlCall.Excel(XlCall.xlcCalculateNow) 抛出 XlCallException

    我有一个 ExcelFunction 来排队一些计算 ExcelFunction public static void QueueCalcs takes ranges var calcRequests builds list of calc
  • std::list::clear 是否会使 std::list::end 迭代器无效?

    检查这个代码 include stdafx h include
  • Nullable 是不可能的,为什么不呢? [复制]

    这个问题在这里已经有答案了 如果这是一个愚蠢的问题 请原谅 我正在尝试更好地理解 Net 中的 Nullable 类型 从我从 Microsoft 源代码 使用 ReSharper 中注意到的内容 我了解到 Nullable 是一个结构 而
  • 当其源是 https uri 时如何使 wpf MediaElement 播放

    在 wpf 独立应用程序 exe 中 我在主窗口中包含了 MediaElement
  • EventHandler 应该始终用于事件吗?

    我一直在愉快地使用自定义委托类型和通用编写事件Action委托类型 没有真正考虑我在做什么 我有一些很好的扩展助手Action and EventHandler这使我倾向于使用那些预定义的委托类型而不是我自己的委托类型 但除此之外 除了惯例
  • OpenGL缓冲区更新[重复]

    这个问题在这里已经有答案了 目前我正在编写一个模拟水的程序 以下是我所做的步骤 创建水面 平面 创建VAO 创建顶点缓冲区对象 在其中存储法线和顶点 将指针绑定到此 VBO 创建索引缓冲区对象 然后我使用 glDrawElements 渲染
  • .pdbs 会减慢发布应用程序的速度吗?

    如果 dll 中包含 pdb 程序调试 文件 则行号将出现在引发的任何异常的堆栈跟踪中 这会影响应用程序的性能吗 这个问题与发布与调试 即优化 无关 这是关于拥有 pdb 文件的性能影响 每次抛出异常时都会读取 pdb 文件吗 加载程序集时
  • C# Outlook 从收件人获取 CompanyName 属性

    我目前正在使用 C 编写 Outlook 2010 AddIn 我想要的是从我从 AppointmentItem 中提取的 Recipient 对象中获取 CompanyName 属性 因此 有了 AppointmentItem 的收件人
  • Javascript:我应该隐藏我的实现吗?

    作为一名 C 程序员 我有一个习惯 将可以而且应该私有的东西设为私有 当 JS 类型向我公开其所有私有部分时 我总是有一种奇怪的感觉 而且这种感觉并没有被 唤起 假设我有一个类型draw方法 内部调用drawBackground and d
  • 具有多个谓词的 C++11 算法

    功能如std find if来自algorithmheader 确实很有用 但对我来说 一个严重的限制是我只能为每次调用使用 1 个谓词count if 例如给定一个像这样的容器std vector我想同时应用相同的迭代find if 多个
  • 如何查明 .exe 是否正在 C++ 中运行?

    给定进程名称 例如 程序 exe C 标准库没有这样的支持 您需要一个操作系统 API 来执行此操作 如果这是 Windows 那么您将使用 CreateToolhelp32Snapshot 然后使用 Process32First 和 Pr
  • 以下 PLINQ 代码没有改进

    我没有看到使用以下代码的处理速度有任何改进 IEnumerable
  • 虚拟并行端口模拟器

    在我的计算机网络课程中 我们应该通过使用本机寄存器 例如使用 outportb 等命令 来学习并行端口编程 我没有并行端口 因为我住在 2011 年 但想练习这些程序 我使用 dosbox 安装了旧的 Turboc 3 IDE 有没有一个程
  • 为什么 std::function 不是有效的模板参数,而函数指针却是?

    我已经定义了名为的类模板CallBackAtInit其唯一目的是在初始化时调用函数 构造函数 该函数在模板参数中指定 问题是模板不接受std function作为参数 但它们接受函数指针 为什么 这是我的代码 include
  • 如何使用 C# 查询远程 MS ACCESS .mdb 数据库

    我正在尝试使用 C 查询 mote MS ACCESS 数据库 mdb 文件 将文件复制到本地计算机时可以成功查询它 我只想远程放置文件 所以我的客户端程序不包含原始数据 static string m path http www xyz
  • 将 2 个字节转换为整数

    我收到一个 2 个字节的端口号 最低有效字节在前 我想将其转换为整数 以便我可以使用它 我做了这个 char buf 2 Where the received bytes are char port 2 port 0 buf 1 port
  • WPF DataGrid - 在每行末尾添加按钮

    我想在数据网格的每一行的末尾添加一个按钮 我找到了以下 xaml 但它将按钮添加到开头 有人知道如何在所有数据绑定列之后添加它吗 这会将按钮添加到开头而不是末尾
  • 在 Qt 中播放通知(频率 x)声音 - 最简单的方法?

    Qt 5 1 或更高版本 我需要播放频率为 x 的通知声音 n 毫秒 如果我能像这样组合音调那就太好了 1000Hz 持续 2 秒 然后 3000Hz 持续 1 秒 最简单的方法是使用文件 WAV MP3 例如如此处所述 如何用Qt播放声音
  • 用数组或向量实现多维数组

    我想使用单个数组或向量实现多维数组 可以像通常的多维数组一样访问它 例如 a 1 2 3 我陷入困境的是如何实施 操作员 如果数组的维数为 1 则 a 1 应该返回位于索引 1 处的元素 但是如果维数大于一怎么办 对于嵌套向量 例如 3 维
  • 解释这段代码的工作原理;子进程如何返回值以及在哪里返回值?

    我不明白子进程如何返回该值以及返回给谁 输出为 6 7 问题来源 http www cs utexas edu mwalfish classes s11 cs372h hw sol1 html http www cs utexas edu

随机推荐

  • 如何使用 Jersey 的内部路由机制来提取类/方法引用?

    我正在运行 Jersey 1 8 应用程序 Jersey 作为 Servlet 运行 我需要写一个小服务程序过滤器给定一个简单的请求 响应 能够确定哪个 REST 资源 方法将响应该请求并从注释中提取值 例如 假设我有以下资源 Path f
  • ruby on Rails 中的自动测试错误

    我运行了命令自动测试 这是我得到的错误 我正在关注 Hartl 的书 想知道这种冲突是否会发生 因为 Rails 现在附带了 ZenTest 还是其他东西 我怎样才能克服这个错误 我是 RoR 新手 Invalid gemspec in U
  • Flutter FireAuth 删除 reCAPTCHA 横幅(WEB)

    我正在创建一个 flutter web 应用程序 并使用 firebase auth 通过电话号码对用户进行身份验证 await auth signInWithPhoneNumber phoneNumber 执行此命令后 页面右下角会出现一
  • 嵌套 for 循环以列出具有不同“if”条件的理解

    我正在尝试将此嵌套循环转换为列表理解 但我不确定是否可能 因为 tmp 列表中的项目可能有不同的值 这是最好的方法吗 谢谢 final for a in range 13 1 for b in range 0 4 for c in rang
  • 使用不同间隔的任务运行 DAG

    我有 3 个任务 A B 和 C 我只想运行任务 A 一次 然后每月运行任务 B 直到 end date 然后仅运行任务 C 一次以进行清理 这与这个问题类似 但不适用 如何在气流中的单个 Dag 上处理不同的任务间隔 https stac
  • 从 Unity WebGL 调用 Angular2 函数

    目前 我正在使用 Angular2 版本 2 1 2 和 Unity 可视化工具 使用 Unity 5 5 构建 我需要做的是从 Unity 到 Angular2 进行通信 我正在使用类似于下面的代码 public void GetBill
  • 将最低部署目标升级到 iOS 9 后无法在 Xcode 9 中使用安全区域

    我刚刚将我的应用程序从支持 iOS 8 及更高版本更改为支持 iOS 9 及更高版本 我相信我已经正确完成了此操作 因为当我现在构建应用程序时 我会收到有关应用程序中已在 iOS 9 中弃用的所有内容的警告 我正在使用 Xcode 9 GM
  • 以系统帐户运行exe

    我正在尝试将我的 c exe 作为系统帐户运行 我怎样才能做到这一点 我试过了
  • 关于 servlet 的简要想法[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 从哪里可以获得有关 servlet 的知识 大多数人会从 Sun 的有关 servlet 的官方教程开
  • 使用 UpdatePanel 的 ASP.NET AJAX

    从概念上讲 我对 AJAX 的理解是异步发送到服务器的请求 也称为并行 当我使用多个UpdatePanels在页面上并触发多个异步回发 例如通过使用按钮 我注意到第二个请求在第一个请求完成之前才开始 但是当我使用 JQuery ajax 并
  • 您可以bind()和connect() UDP连接的两端吗

    我正在编写一个点对点消息队列系统 它必须能够通过 UDP 运行 我可以任意选择一侧或另一侧作为 服务器 但这似乎不太正确 因为两端都从另一端发送和接收相同类型的数据 是否可以绑定 和连接 两端 以便它们只能彼此发送 接收 这似乎是一种非常对
  • WPF 的拖放列表框

    我正在寻找一个简单的ListBox具有内置的拖放功能 我认为 Silverlight 4 工具包有 The BoxList应该可以 通过拖放项目重新排序 从一个项目中拖动项目BoxList到另一个 显示拖动项目的预览 幽灵版本 显示放置位置
  • npx create-react-app myapp 命令抛出错误

    我想在 React 中创建一个应用程序 我已经安装了最新的 Node js 当我运行命令时出现错误 PS C Users Kumar Sanket Desktop React Redux gt npx create react app my
  • 对二进制数的字符串表示进行按位运算 python 2.7

    我想对二进制数的两个字符串表示执行按位或 但我不知道如何将字符串转换为原始二进制 a 010110 b 100000 a b 应该产生 110110 然后我想计算 on 位的数量 这应该返回 4 您可以使用内置的将字符串转换为二进制int
  • ggplot2:从纵横比中排除图例

    I use ggplot2 and knitr发布带有右侧图例的散点图 图例包含在纵横比中 因此破坏了绘图的 方形 如图所示默认主题 https github com hadley ggplot2 wiki themes 当图例文本变得比
  • Flutter中向TabView添加选项卡标签

    我正在尝试扩展作为答案提出的 TabViewhere https stackoverflow com questions 50036546 how to create a dynamic tabbarview render a new ta
  • 当属性值在 HTML5 中可以保持不带引号时

    HTML5 中什么时候属性值可以保持不带引号 HTML4 01 是一个 SGML 应用程序 因此 在 HTML4 中 如果值中使用的唯一字符是当前声明为名称字符的字符 字母数字字符 句号 则可以省略引号 好吧 来自 W3C 工作草案 201
  • Angular2 找不到命名空间“google”

    我正在与angular2 google maps以及最新版本的 Angular2 我正在尝试将一些本地地图组件功能转换为自己文件中的服务maps service ts 例如 地图组件 ts getGeoLocation lat number
  • 让 Selenium 与 Bootstrap 模式淡入淡出配合的建议?

    我正在努力以 BDD 的方式生活 我正在使用 Cucumber 带有 Selenium 并且碰巧在我的应用程序中使用 Twitter Bootstrap 模式 在运行 Cucumber 测试时 我得到了 Selenium WebDriver
  • 错误 - 仅返回类型不同的函数不能重载。由 小码哥发布于

    我正在尝试创建一个图书馆管理系统 我收到一些我不明白的错误 我在 Mac 操作系统中使用 Eclipse 我的主要代码是 include