|
查看: 1695|回复: 5
|
'class' type redefinition??怎么解决?
[复制链接]
|
|
|
这是我根据书本打出来的
有好多errors可是debug剩下一个
试过了也上网找了相关资料
好像是因为在多个file里include到gameType.h
是不是要用#ifndef,#define,#endif 这些东西才可以?
可是我试过了不行
应该是不知道要怎么用它吧
懊恼ing...
可否请大家赐教?
拜托了。。。
main.cpp
- #include <iostream>
- #include <fstream>
- #include <string>
- #include "GameType.h"
- #include "gameListType.h"
- using namespace std;
- void createVideoList(ifstream& infile, gameListType& gameList);
- void displayMenu();
- int main()
- {
- gameListType gameList;
- int choice;
- char ch;
- string title;
- ifstream infile;
- infile.open("c:\\gamelist.txt");
- if(!infile)
- {
- cerr<<"Input file does not exist"<<endl;
- return 1;
- }
- // createGameList(infile, gameList);
- void createGameList(ifstream& infile, gameListType& gameList);
- infile.close();
- displayMenu();
- cout<<"Enter your choice: ";
- cin>>choice;
- cin.get(ch);
- cout<<endl;
- while(choice != 0)
- {
- switch(choice)
- {
- case 1:
- cout<<"Enter the title: ";
- getline(cin, title);
- cout<<endl;
- if(gameList.gameSearch(title))
- cout<<"Title found"<<endl;
- else
- cout<<"The store does not carry this title."<<endl;
- break;
- case 2:
- cout<<"Enter the title: ";
- getline(cin, title);
- cout<<endl;
- if(gameList.gameSearch(title))
- {
- if(gameList.isGameAvailable(title))
- {
- gameList.gameCheckOut(title);
- cout<<"Enjoy your game: "<<title<<endl;
- }
- else
- cout<<"The game is currently"
- <<"out of stock."<<endl;
- }
- else
- cout<<"The game is not in the store."<<endl;
- break;
- case 3:
- cout<<"Enter the title: ";
- getline(cin, title);
- cout<<endl;
- if(gameList.gameSearch(title))
- {
- gameList.gameCheckIn(title);
- cout<<"Thanks for returning "<<title<<endl;
- }
- else
- cout<<"This game is not from our store."<<endl;
- break;
- case 4:
- cout<<"Enter the title: ";
- getline(cin, title);
- cout<<endl;
- if(gameList.gameSearch(title))
- {
- if(gameList.isGameAvailable(title))
- {
- cout<<"The game is currently in stock."<<endl;
- }
- else
- cout<<"The game is out of stock."<<endl;
- }
- else
- cout<<"The game is not in the store."<<endl;
- break;
-
- case 5:
- gameList.gamePrintTitle();
- break;
- case 6:
- cout<<gameList<<endl;
- break;
- default : cout<<"Bad Selection"<<endl;
- }//end switch
- displayMenu();
- cout<<"Enter your choice: ";
- cin>>choice;
- cin.get(ch);
- cout<<endl;
- }//end while
- return 0;
- }
- void createGameList(ifstream& infile, gameListType& gameList)
- {
- string Title;
- string Star1;
- string Star2;
- string Producer;
- string Director;
- string ProductionCo;
- char ch;
- int InStock;
- gameType newGame;
- getline(infile, Title);
- while(infile)
- {
- getline(infile, Star1);
- getline(infile, Star2);
- getline(infile, Producer);
- getline(infile, Director);
- getline(infile, ProductionCo);
- infile>>InStock;
- infile.get(ch);
- newGame.setGameInfo(Title,Star1,Star2,Producer,Director, ProductionCo,InStock);
- gameList.insertFirst(newGame);
- getline(infile, Title);
- }
- }
- void displayMenu()
- {
- cout<<"\t\t\tWELCOME To GAME WORLD"<<endl;
- cout<<"\t\t\t-------------------------------"<<endl;
- cout<<"\t\t\t**************MENU*************"<<endl;
- cout<<"\t\t\tEnter 1 To check whether a particular games is in the store"<<endl;
- cout<<"\t\t\tEnter 2 To check out a game"<<endl;
- cout<<"\t\t\tEnter 3 To check in a game"<<endl;
- cout<<"\t\t\tEnter 4 To check whether a particular games is in the stock"<<endl;
- cout<<"\t\t\tEnter 5 To print the titles of all the games"<<endl;
- cout<<"\t\t\tEnter 6 To print a list of all the games"<<endl;
- cout<<"\t\t\tEnter 0 To Exit"<<endl;
- }
复制代码
gameType.h
-
- #include <iostream>
- #include<string>
- using namespace std;
- class gameType
- {
- string gameTitle;
- string gStar1;
- string gStar2;
- string gProducer;
- string gDirector;
- string gProductionCo;
- int copiesInStock;
- friend ostream& operator<<(ostream& os, const gameType& game)
- {
- os<<endl;
- os<<"Game Title:"<<game.gameTitle<<endl;
- os<<"Stars:"<<game.gStar1<<" and "<<game.gStar2<<endl;
- os<<"Producer:"<<game.gProducer<<endl;
- os<<"Director:"<<game.gDirector<<endl;
- os<<"Production Company: "<<game.gProductionCo<<endl;
- os<<"Copies in stock: "<<game.copiesInStock<<endl;
- os<<"_________________________________________"<<endl;
- return os;
- }
- public:
- void setGameInfo(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
- {
- gameTitle=title;
- gStar1=star1;
- gStar2=star2;
- gProducer=producer;
- gDirector=director;
- gProductionCo=productionCo;
- copiesInStock=setInStock;
- }
- int getNoOfCopiesInStock() const
- {
- return copiesInStock;
- }
- void checkOut()
- {
- if (getNoOfCopiesInStock()>0)
- copiesInStock--;
- else
- cout<<"Currently out of stock"<<endl;
- }
- void checkIn()
- {
- copiesInStock++;
- }
- void printTitle() const
- {
- cout<<"Game Title: "<<gameTitle<<endl;
- }
- void printInfo() const
- {
- cout<<"Game Title: "<<gameTitle<<endl;
- cout<<"Stars: "<<gStar1<<" and "<<gStar2<<endl;
- cout<<"Producer: "<<gProducer<<endl;
- cout<<"Director: "<<gDirector<<endl;
- cout<<"Production Company: "<<gProductionCo<<endl;
- cout<<"Copies in stock: "<<copiesInStock<<endl;
- }
- bool checkTitle(string title)
- {
- return(gameTitle==title);
- }
- void updateInStock(int num)
- {
- copiesInStock+=num;
- }
- void setCopiesInStock(int num)
- {
- copiesInStock=num;
- }
- string getTitle()
- {
- return gameTitle;
- }
- gameType(){}
- gameType(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
- {
- setGameInfo( title, star1, star2, producer, director, productionCo, setInStock);
- }
- bool operator==(const gameType& other) const
- {
- return (gameTitle==other.gameTitle);
- }
- bool operator!=(const gameType& other) const
- {
- return (gameTitle != other.gameTitle);
- }
- };
复制代码
gametype.h(7) : error C2011: 'gameType' : 'class' type redefinition
请大家救命。紧急!~ |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-4-2009 07:48 AM
|
显示全部楼层
gameListType.h
- #include <iostream>
- #include <string>
- #include "linkedList.h"
- #include "gameType.h"
- using namespace std;
- class gameListType : public linkedListType<gameType>
- {
- public:
- bool gameSearch(string gTitle);
- bool isGameAvailable(string gTitle);
- void gameCheckOut(string gTitle);
- void gameCheckIn(string gTitle);
- bool gameCheckTitle(string gTitle);
- void gameUpdateInStock(string gTitle, int num);
- void gameSetCopiesInStock(string gTitle, int num);
- void gamePrintTitle();
- private:
- void searchGameList(string gTitle, bool& found, nodeType<gameType>* ¤t);
- };
- void gameListType::searchGameList(string gTitle, bool& found, nodeType<gameType>* ¤t)
- {
- found = false;
- if(first == NULL)
- cerr<<"Cannot search an empty list."<<endl;
- else
- {
- current = first;
- found = false;
- while(!found && current != NULL)
- if(current -> info.checkTitle(gTitle))
- found = true;
- else
- current = current-> link;
- }
- }
- bool gameListType::isGameAvailable(string gTitle)
- {
- bool found;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- if(found)
- found = (location ->info.getNoOfCopiesInStock() > 0);
- else
- found = false;
-
- return found;
- }
- void gameListType::gameCheckIn(string gTitle)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
-
- if(found)
- location -> info.checkIn();
- else
- cout<<"The store does not carry this game."<<endl;
- }
- void gameListType::gameCheckOut(string gTitle)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- if(found)
- location -> info.checkOut();
- else
- cout<<"The store does not carry this game."<<endl;
- }
- bool gameListType::gameCheckTitle(string gTitle)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- return found;
- }
- void gameListType::gameUpdateInStock(string gTitle, int num)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- if(found)
- location -> info.updateInStock(num);
- else
- cout<<"The store does not carry this game."<<endl;
- }
- void gameListType::gameSetCopiesInStock(string gTitle, int num)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- if(found)
- location -> info.setCopiesInStock(num);
- else
- cout<<"The store does not carry this game."<<endl;
- }
- bool gameListType::gameSearch(string gTitle)
- {
- bool found = false;
- nodeType<gameType> *location;
- searchGameList(gTitle, found, location);
- return found;
- }
- void gameListType::gamePrintTitle()
- {
- nodeType<gameType>* current;
- current=first;
- while(current!=NULL)
- {
- current->info.printTitle();
- current = current ->link;
- }
- }
复制代码
linkedList.h
|
|
|
|
|
|
|
|
|
|
|
发表于 13-4-2009 10:57 AM
|
显示全部楼层
你知不知道 #include 的真正意义?
你知不知道 header file 的真正意义?
你没有解释你如何应用 #ifndef #define 和 #endif,叫人如何判断你用得对不对呢?
改次记得善用孤狗: http://www.cplusplus.com/forum/beginner/4915/
简单来说,这个error是因为你include了某个file,里面重复了你已经define过的class,也就是 gameType 这个class。
你在 Main 里面 include 了 gameType.h 和 gameListType.h ,然后又在 gameListType.h 里面再次 include gameType.h。。。。
[ 本帖最后由 geekman 于 13-4-2009 11:16 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 14-4-2009 08:23 PM
|
显示全部楼层
原帖由 geekman 于 13-4-2009 10:57 AM 发表 
你知不知道 #include 的真正意义?
你知不知道 header file 的真正意义?
你没有解释你如何应用 #ifndef #define 和 #endif,叫人如何判断你用得对不对呢?
改次记得善用孤狗: http://www.cplusplus.com/f ...
为什么酱多人喜欢用填鸭法来学编程 |
|
|
|
|
|
|
|
|
|
|
发表于 15-4-2009 02:00 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 1-5-2009 09:32 PM
|
显示全部楼层
你的 main.cpp include 了:
GameType.h 及
gameListType.h
但同時你的 gameListType.h 又 include 了
GameType.h
我沒有 Compile 你的 code。但根據經驗,你可以:
1。在 main.cpp 只 include GameListype.h 就夠了。或
2。在 GameType.h 加
#ifndef abc
#define abc
.... <the rest of you code goes here>
#endif
3。新的 Compiler 應該可以用 #pragma once
舊的要在你的 GameType.h |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|