佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1695|回复: 5

'class' type redefinition??怎么解决?

[复制链接]
发表于 13-4-2009 07:48 AM | 显示全部楼层 |阅读模式
这是我根据书本打出来的
有好多errors可是debug剩下一个
试过了也上网找了相关资料
好像是因为在多个file里include到gameType.h
是不是要用#ifndef,#define,#endif 这些东西才可以?
可是我试过了不行
应该是不知道要怎么用它吧
懊恼ing...
可否请大家赐教?
拜托了。。。


main.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "GameType.h"
  5. #include "gameListType.h"
  6. using namespace std;

  7. void createVideoList(ifstream& infile, gameListType& gameList);
  8. void displayMenu();

  9. int main()
  10. {
  11.    gameListType gameList;
  12.    int choice;
  13.    char ch;
  14.    string title;

  15.    ifstream infile;
  16.    infile.open("c:\\gamelist.txt");
  17.    if(!infile)
  18.    {
  19.       cerr<<"Input file does not exist"<<endl;
  20.       return 1;
  21.    }

  22. //   createGameList(infile, gameList);
  23.    void createGameList(ifstream& infile, gameListType& gameList);
  24.    infile.close();

  25.    displayMenu();
  26.    cout<<"Enter your choice: ";
  27.    cin>>choice;
  28.    cin.get(ch);
  29.    cout<<endl;

  30.    while(choice != 0)
  31.    {
  32.       switch(choice)
  33.       {
  34.       case 1:
  35.          cout<<"Enter the title: ";
  36.          getline(cin, title);
  37.          cout<<endl;
  38.          if(gameList.gameSearch(title))
  39.             cout<<"Title found"<<endl;
  40.          else
  41.             cout<<"The store does not carry this title."<<endl;

  42.          break;

  43.       case 2:
  44.          cout<<"Enter the title: ";
  45.          getline(cin, title);
  46.          cout<<endl;
  47.          if(gameList.gameSearch(title))
  48.          {
  49.             if(gameList.isGameAvailable(title))
  50.             {
  51.                gameList.gameCheckOut(title);
  52.                cout<<"Enjoy your game: "<<title<<endl;
  53.             }
  54.             else
  55.                cout<<"The game is currently"
  56.                   <<"out of stock."<<endl;
  57.          }
  58.          else
  59.             cout<<"The game is not in the store."<<endl;
  60.          break;

  61.       case 3:
  62.          cout<<"Enter the title: ";
  63.          getline(cin, title);
  64.          cout<<endl;
  65.          if(gameList.gameSearch(title))
  66.          {
  67.             gameList.gameCheckIn(title);
  68.             cout<<"Thanks for returning "<<title<<endl;
  69.          }
  70.          else
  71.             cout<<"This game is not from our store."<<endl;
  72.          break;

  73.       case 4:
  74.          cout<<"Enter the title: ";
  75.          getline(cin, title);
  76.          cout<<endl;
  77.          if(gameList.gameSearch(title))
  78.          {
  79.             if(gameList.isGameAvailable(title))
  80.             {
  81.                cout<<"The game is currently in stock."<<endl;
  82.             }
  83.             else
  84.                cout<<"The game is out of stock."<<endl;
  85.          }
  86.          else
  87.             cout<<"The game is not in the store."<<endl;
  88.          break;
  89.       
  90.       case 5:
  91.          gameList.gamePrintTitle();
  92.          break;

  93.       case 6:
  94.          cout<<gameList<<endl;
  95.          break;

  96.       default : cout<<"Bad Selection"<<endl;
  97.       }//end switch

  98.       displayMenu();
  99.       cout<<"Enter your choice: ";
  100.       cin>>choice;
  101.       cin.get(ch);
  102.       cout<<endl;
  103.    }//end while

  104.    return 0;
  105. }

  106. void createGameList(ifstream& infile, gameListType& gameList)
  107. {
  108.    string Title;
  109.    string Star1;
  110.    string Star2;
  111.    string Producer;
  112.    string Director;
  113.    string ProductionCo;
  114.    char ch;
  115.    int InStock;

  116.    gameType newGame;

  117.    getline(infile, Title);
  118.    while(infile)
  119.    {
  120.       getline(infile, Star1);
  121.       getline(infile, Star2);
  122.       getline(infile, Producer);
  123.       getline(infile, Director);
  124.       getline(infile, ProductionCo);
  125.       infile>>InStock;
  126.       infile.get(ch);
  127.       newGame.setGameInfo(Title,Star1,Star2,Producer,Director, ProductionCo,InStock);
  128.       gameList.insertFirst(newGame);

  129.       getline(infile, Title);
  130.    }
  131. }

  132. void displayMenu()
  133. {
  134.       cout<<"\t\t\tWELCOME To GAME WORLD"<<endl;
  135.       cout<<"\t\t\t-------------------------------"<<endl;
  136.       cout<<"\t\t\t**************MENU*************"<<endl;
  137.       cout<<"\t\t\tEnter 1 To check whether a particular games is in the store"<<endl;
  138.       cout<<"\t\t\tEnter 2 To check out a game"<<endl;
  139.       cout<<"\t\t\tEnter 3 To check in a game"<<endl;
  140.       cout<<"\t\t\tEnter 4 To check whether a particular games is in the stock"<<endl;
  141.       cout<<"\t\t\tEnter 5 To print the titles of all the games"<<endl;
  142.       cout<<"\t\t\tEnter 6 To print a list of all the games"<<endl;
  143.       cout<<"\t\t\tEnter 0 To Exit"<<endl;
  144. }
复制代码



gameType.h

  1. #include <iostream>
  2. #include<string>

  3. using namespace std;

  4. class gameType
  5. {
  6.    string gameTitle;
  7.    string gStar1;
  8.    string gStar2;
  9.    string gProducer;
  10.    string gDirector;
  11.    string gProductionCo;

  12.    int copiesInStock;

  13.    friend ostream& operator<<(ostream& os, const gameType& game)
  14.    {
  15.       os<<endl;
  16.       os<<"Game Title:"<<game.gameTitle<<endl;
  17.       os<<"Stars:"<<game.gStar1<<" and "<<game.gStar2<<endl;
  18.       os<<"Producer:"<<game.gProducer<<endl;
  19.       os<<"Director:"<<game.gDirector<<endl;
  20.       os<<"Production Company: "<<game.gProductionCo<<endl;
  21.       os<<"Copies in stock: "<<game.copiesInStock<<endl;
  22.       os<<"_________________________________________"<<endl;
  23.       return os;
  24.    }

  25. public:

  26.    void setGameInfo(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
  27.    {
  28.       gameTitle=title;
  29.       gStar1=star1;
  30.       gStar2=star2;
  31.       gProducer=producer;
  32.       gDirector=director;
  33.       gProductionCo=productionCo;
  34.       copiesInStock=setInStock;
  35.    }

  36.    int getNoOfCopiesInStock() const
  37.    {
  38.       return copiesInStock;
  39.    }

  40.    void checkOut()
  41.    {
  42.       if (getNoOfCopiesInStock()>0)
  43.          copiesInStock--;
  44.       else
  45.          cout<<"Currently out of stock"<<endl;
  46.    }

  47.    void checkIn()
  48.    {
  49.          copiesInStock++;
  50.    }

  51.    void printTitle() const
  52.    {
  53.       cout<<"Game Title: "<<gameTitle<<endl;
  54.    }

  55.    void printInfo() const
  56.    {
  57.       cout<<"Game Title: "<<gameTitle<<endl;
  58.       cout<<"Stars: "<<gStar1<<" and "<<gStar2<<endl;
  59.       cout<<"Producer: "<<gProducer<<endl;
  60.       cout<<"Director: "<<gDirector<<endl;
  61.       cout<<"Production Company: "<<gProductionCo<<endl;
  62.       cout<<"Copies in stock: "<<copiesInStock<<endl;
  63.    }

  64.    bool checkTitle(string title)
  65.    {
  66.       return(gameTitle==title);
  67.    }

  68.    void updateInStock(int num)
  69.    {
  70.       copiesInStock+=num;
  71.    }

  72.    void setCopiesInStock(int num)
  73.    {
  74.       copiesInStock=num;
  75.    }

  76.    string getTitle()
  77.    {
  78.       return gameTitle;
  79.    }

  80.    gameType(){}

  81.    gameType(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
  82.    {
  83.       setGameInfo( title, star1, star2, producer, director,  productionCo,  setInStock);
  84.    }

  85.    bool operator==(const gameType& other) const
  86.    {
  87.       return (gameTitle==other.gameTitle);
  88.    }

  89.    bool operator!=(const gameType& other) const
  90.    {
  91.       return (gameTitle != other.gameTitle);
  92.    }
  93. };

复制代码



gametype.h(7) : error C2011: 'gameType' : 'class' type redefinition

请大家救命。紧急!~
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 13-4-2009 07:48 AM | 显示全部楼层
gameListType.h

  1. #include <iostream>
  2. #include <string>
  3. #include "linkedList.h"
  4. #include "gameType.h"
  5. using namespace std;

  6. class gameListType : public linkedListType<gameType>
  7. {
  8. public:
  9.    bool gameSearch(string gTitle);
  10.    bool isGameAvailable(string gTitle);
  11.    void gameCheckOut(string gTitle);
  12.    void gameCheckIn(string gTitle);
  13.    bool gameCheckTitle(string gTitle);
  14.    void gameUpdateInStock(string gTitle, int num);
  15.    void gameSetCopiesInStock(string gTitle, int num);
  16.    void gamePrintTitle();

  17. private:
  18.    void searchGameList(string gTitle, bool& found, nodeType<gameType>* &current);
  19. };

  20. void gameListType::searchGameList(string gTitle, bool& found, nodeType<gameType>* &current)
  21. {
  22.    found = false;

  23.    if(first == NULL)
  24.       cerr<<"Cannot search an empty list."<<endl;
  25.    else
  26.    {
  27.       current = first;
  28.       found = false;

  29.       while(!found && current != NULL)
  30.          if(current -> info.checkTitle(gTitle))
  31.             found = true;
  32.          else
  33.             current = current-> link;
  34.    }
  35. }

  36. bool gameListType::isGameAvailable(string gTitle)
  37. {
  38.    bool found;
  39.    nodeType<gameType> *location;
  40.    searchGameList(gTitle, found, location);

  41.    if(found)
  42.       found = (location ->info.getNoOfCopiesInStock() > 0);
  43.    else
  44.       found = false;
  45.    
  46.    return found;
  47. }

  48. void gameListType::gameCheckIn(string gTitle)
  49. {
  50.    bool found = false;
  51.    nodeType<gameType> *location;

  52.    searchGameList(gTitle, found, location);
  53.    
  54.    if(found)
  55.       location -> info.checkIn();
  56.    else
  57.       cout<<"The store does not carry this game."<<endl;
  58. }

  59. void gameListType::gameCheckOut(string gTitle)
  60. {
  61.    bool found = false;
  62.    nodeType<gameType> *location;

  63.    searchGameList(gTitle, found, location);

  64.    if(found)
  65.       location -> info.checkOut();
  66.    else
  67.       cout<<"The store does not carry this game."<<endl;
  68. }

  69. bool gameListType::gameCheckTitle(string gTitle)
  70. {
  71.    bool found = false;
  72.    nodeType<gameType> *location;

  73.    searchGameList(gTitle, found, location);

  74.    return found;
  75. }

  76. void gameListType::gameUpdateInStock(string gTitle, int num)
  77. {
  78.    bool found = false;
  79.    nodeType<gameType> *location;

  80.    searchGameList(gTitle, found, location);

  81.    if(found)
  82.       location -> info.updateInStock(num);
  83.    else
  84.       cout<<"The store does not carry this game."<<endl;
  85. }

  86. void gameListType::gameSetCopiesInStock(string gTitle, int num)
  87. {
  88.    bool found = false;
  89.    nodeType<gameType> *location;

  90.    searchGameList(gTitle, found, location);

  91.    if(found)
  92.       location -> info.setCopiesInStock(num);
  93.    else
  94.       cout<<"The store does not carry this game."<<endl;
  95. }

  96. bool gameListType::gameSearch(string gTitle)
  97. {
  98.    bool found = false;
  99.    nodeType<gameType> *location;

  100.    searchGameList(gTitle, found, location);

  101.    return found;
  102. }

  103. void gameListType::gamePrintTitle()
  104. {
  105.    nodeType<gameType>* current;

  106.    current=first;
  107.    while(current!=NULL)
  108.    {
  109.       current->info.printTitle();
  110.       current = current ->link;
  111.    }
  112. }
复制代码


linkedList.h

  1. template <class Type>
  2. struct nodeType
  3. {
  4.    Type info;
  5.    nodeType<Type>*link;
  6. };

  7. template <class Type>
  8. class linkedListType
  9. {
  10.    friend ostream& operator<<(ostream&, const linkedListType<Type>&);

  11. public:
  12.    const linkedListType<Type>& operator=(const linkedListType<Type>&);
  13.    void initializeList();
  14.    int length();
  15.    void destroyList();
  16.    bool isEmptyList();
  17.    Type back();
  18.    Type front();
  19.    bool search(const Type& searchItem);
  20.    void insertLast(const Type& newItem);
  21.    void deleteNode(const Type& deleteItem);
  22.    void insertFirst(const Type& newItem);
  23.    linkedListType();
  24.    linkedListType(const linkedListType<Type>& otherList);
  25.    ~linkedListType();

  26. protected:
  27.    int count;
  28.    nodeType<Type>*first;
  29.    nodeType<Type>*last;

  30. private:
  31.    void copyList(const linkedListType<Type>& otherList);
  32. };


  33. template <class Type>
  34. bool linkedListType<Type>::isEmptyList()
  35. {
  36.    return(first==NULL);
  37. }

  38. //Default Constructor
  39. template <class Type>
  40. linkedListType<Type>::linkedListType()
  41. {
  42.    first=NULL;
  43.    last=NULL;
  44.    count=0;
  45. }


  46. //Destroy list
  47. template <class Type>
  48. void linkedListType<Type>::destroyList()
  49. {
  50.    nodeType<Type>*temp;
  51.    while (first!=NULL)
  52.    {
  53.       temp=first;
  54.       first=first->link;
  55.       delete temp;
  56.    }
  57.    last=NULL;
  58.    count=0;
  59. }

  60. //Initialize List
  61. template <class Type>
  62. void linkedListType<Type>::initializeList()
  63. {
  64.    destroyList();
  65. }


  66. //Overloading the Stream Insertion  Operator
  67. template <class Type>
  68. ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
  69. {
  70.    nodeType<Type>*current;
  71.    current=list.first;
  72.    while(current !=NULL)
  73.    {
  74.       osObject<<current->info<<"";
  75.       current=current->link;
  76.    }
  77.    return osObject;
  78. }


  79. //Length of the List
  80. template <class Type>
  81. int linkedListType<Type>::length()
  82. {
  83.    return count;
  84. }


  85. //Retrieve Data of The First Node
  86. template <class Type>
  87. Type linkedListType<Type>::front()
  88. {
  89.    assert(last!=NULL);
  90.    return first->info;
  91. }


  92. //Retrieve Data of the Last Node
  93. template <class Type>
  94. Type linkedListType<Type>::back()
  95. {
  96.    assert(last!=NULL);
  97.    return last->info;
  98. }


  99. //Search List
  100. template <class Type>
  101. bool linkedListType<Type>::search(const Type& searchItem)
  102. {
  103.    nodeType<Type>*current;
  104.    bool found;
  105.    currrent=first;
  106.    found=false;
  107.    while(current !=NULL && !found)
  108.       if(current->info==searchItem)
  109.          found=true;
  110.       else
  111.          current=current->link;

  112.       return found;
  113. }


  114. //Insert First Node
  115. template <class Type>
  116. void linkedListType<Type>::insertFirst(const Type& newItem)
  117. {
  118.    nodeType<Type>*newNode;
  119.    newNode=new nodeType<Type>;
  120.    assert(newNode !=NULL);
  121.    newNode->info=newItem;
  122.    newNode->link=first;
  123.    first=newNode;
  124.    count ++;
  125.    if(last==NULL)
  126.       last=newNode;
  127. }


  128. //Insert Last Node
  129. template <class Type>
  130. void linkedListType<Type>::insertLast(const Type& newItem)
  131. {
  132.    nodeType<Type>*newNode;
  133.    newNode=new nodeType<Type>;
  134.    assert(newNode !=NULL);
  135.    newNode->info=newItem;
  136.    newNode->link=NULL;
  137.    if(first==NULL)
  138.    {
  139.       first=newNode;
  140.       last=newNode;
  141.       count ++;
  142.    }
  143.    else
  144.    {
  145.       last->link=newNode;
  146.       last=newNode;
  147.       count ++;
  148.    }
  149. }

  150. template <class Type>
  151. void linkedListType<Type>::deleteNode(const Type& deleteItem)
  152. {
  153.    nodeType<Type>*current;
  154.    nodeType<Type>*trailCurrent;
  155.    bool found;

  156.    if (first==NULL)
  157.       cerr<<"cannot delete from an empty list.\n";
  158.    else
  159.    {
  160.       if(first->info==deleteItem)
  161.       {
  162.          current=first;
  163.          first=first->link;
  164.          count --;
  165.          if (first==NULL)
  166.             last=NULL;
  167.          delete current;
  168.       }
  169.       else
  170.       {
  171.          found=false;
  172.          trailCurrent=first;
  173.          current=first->link;

  174.          while(current !=NULL && !found)
  175.          {
  176.             if(current->info !=deleteItem)
  177.             {
  178.                trailCurrent=current;
  179.                current=current->link;
  180.             }
  181.             else
  182.                found = true;
  183.          }

  184.          if(found)
  185.          {
  186.             trailCurrent->link=current->link;
  187.             count --;

  188.             if(last==current)
  189.                last=trailCurrent;
  190.             delete current;
  191.          }
  192.          else
  193.             cout<<"Item to be deleted is not in the list."<<endl;
  194.       }
  195.    }
  196. }



  197. //Copy list
  198. template <class Type>
  199. void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
  200. {
  201.    nodeType<Type>*newNode;
  202.    nodeType<Type>*current;

  203.    if (first !=NULL)
  204.       destroyList();

  205.    if (otherList.first==NULL)
  206.    {
  207.       first=NULL;
  208.       last=NULL;
  209.       count=0;
  210.    }
  211.    else
  212.    {
  213.       current=otherList.first;
  214.       count=otherList.count;

  215.       first=new nodeType<Type>;
  216.       
  217.       assert(first !=NULL);

  218.       first->info=current->info;
  219.       first->link=NULL;

  220.       last=first;

  221.       current=current->link;

  222.       while(current !=NULL)
  223.       {
  224.          newNode = new nodeType<Type>;

  225.          assert(newNode !=NULL);

  226.          newNode->info=current->info;
  227.          newNode->link=NULL;

  228.          last->link=newNode;
  229.          last=newNode;

  230.          current=current->link;
  231.       }
  232.    }
  233. }


  234. //Destructor
  235. template <class Type>
  236. linkedListType<Type>::~linkedListType()
  237. {
  238.    destroyList();
  239. }


  240. //Copy Constructor
  241. template <class Type>
  242. linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
  243. {
  244.    first=NULL;
  245.    copyList(otherList);
  246. }


  247. //Overloading the Assignment operator
  248. template <class Type>
  249. const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
  250. {
  251.    if(this != &otherList)
  252.       copyList(otherList);

  253.    return *this;
  254. }
复制代码
回复

使用道具 举报

发表于 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 | 显示全部楼层
原帖由 yeenfei 于 14-4-2009 08:23 PM 发表

为什么酱多人喜欢用填鸭法来学编程


我也不懂。。。可能老师没有料,所以只能把学生当鸭子来填吧。
回复

使用道具 举报

发表于 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
回复

使用道具 举报

Follow Us
您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 14-12-2025 10:41 PM , Processed in 0.125012 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表