我正在尝试创建一个图书馆管理系统。我收到一些我不明白的错误。我在Mac OS中使用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.
答案 0 :(得分:5)
Game *search( char gameName[]);
DVD *search( char dvdName[]);
Book *search( char bookName[]);
这3个案例中search
的参数具有完全相同的类型。唯一的区别是返回类型。
您不能仅在返回类型上重载,因为调用哪个函数是由参数决定的,而不是由您为其指定的内容决定的。
简单的解决方案是将其称为searchDVD
和searchGame
以及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);
}
}
和Library
:
deferred_search search( char const* name ) { return {this, name}; }
这是一种在返回类型上进行重载的有点模糊的技术。我建议反对它 - 只需调用函数searchTYPE
。
答案 1 :(得分:0)
或者您可以使用可区分的联合 + 字符串结构。 或三个“新”字符串类型。 或者具有三个显式实现的模板化函数。
可能还有更多...
或者您可以使用可区分的联合 + 字符串结构。 或三个“新”字符串类型。 或者具有三个显式实现的模板化函数。
可能还有更多...
例如auto const* result = search(char const* const name); {DVD, Book, Game }
中的任意一种 struct Media { int c; char const name[]; };
struct Book : Media { char const author[]; };
struct DVD : Media { char const director[]; };
struct Game : Media { char const platform[]; };
template struct <class T> MediaTypeId {};
template struct MediaTypeId<Book> { static const id = 0 };
template struct MediaTypeId<DVD> { static const id = 1};
template struct MediaTypeId<Game> { static const id = 2 };
template <class T>
T const* search(char const name[]) const {
// define an array of your 3 search functions
// select the correct search function based upon the index
// obtained via MediaTypeId<T>::id
// call the function and return the result.
// all library functions must return Media const*
};