免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4061 | 回复: 13
打印 上一主题 下一主题

[C++] 一段代码VC下编译通过,GCC则不行为什么 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-24 16:13 |只看该作者 |倒序浏览
就是这段代码。GCC 下出现如下问题  
125  expected `;' before "ref" ,
也就是在这句          list<T>::const_iterator ref;

但是在VC6。0 VC 2005下都没有问题的。请问是什么原因呢?是不是模板的原因呢?
请各位帮忙解答。谢谢


附件是原代码。

  1. #include <iostream>
  2. #include<string.h>
  3. #include <string>
  4. #include <list>
  5. #include <algorithm>

  6. using namespace std;

  7. class Patron;        // forward declaration;//

  8. class Book {
  9. public:
  10.     Book() {
  11.         patron = 0;
  12.     }
  13.     bool operator== (const Book& bk) const {
  14.         return strcmp(title,bk.title) == 0;
  15.     }
  16. private:
  17.     char *title;
  18.     Patron *patron;
  19.  ostream& printBook(ostream&) const;
  20.     friend ostream& operator<< (ostream& out, const Book& bk) {
  21.          return bk.printBook(out);
  22.  }
  23.     friend class CheckedOutBook;
  24.  friend class Patron;
  25.     friend void includeBook();
  26.     friend void checkOutBook();
  27.     friend void returnBook();
  28. };

  29. class Author {
  30. public:
  31.     Author() {
  32.     }
  33.     bool operator== (const Author& ar) const {
  34.         return strcmp(name,ar.name) == 0;
  35.     }
  36. private:
  37.     char *name;
  38.     list<Book> books;
  39.  ostream& printAuthor(ostream&) const;
  40.     friend ostream& operator<< (ostream& out,const Author& ar) {
  41.          return ar.printAuthor(out);
  42.  }
  43.     friend void includeBook();
  44.     friend void checkOutBook();
  45.     friend void returnBook();
  46.     friend class CheckedOutBook;
  47.  friend class Patron;
  48. };

  49. class CheckedOutBook {
  50. public:
  51.     CheckedOutBook(list<Author>::iterator ar = 0,
  52.                             list<Book>::iterator bk = 0) {
  53.         author = ar;
  54.          book = bk;
  55.     }
  56.     bool operator== (const CheckedOutBook& bk) const {
  57.         return strcmp(author->name,bk.author->name) == 0 &&
  58.                strcmp(book->title,bk.book->title) == 0;
  59.     }
  60. private:
  61.     list<Author>::iterator author;
  62.     list<Book>::iterator book;
  63.     friend void checkOutBook();
  64.     friend void returnBook();
  65.  friend class Patron;
  66. };

  67. class Patron {
  68. public:
  69.     Patron() {
  70.     }
  71.     bool operator== (const Patron& pn) const {
  72.         return strcmp(name,pn.name) == 0;
  73.     }
  74. private:
  75.     char *name;
  76.     list<CheckedOutBook> books;
  77.  ostream& printPatron(ostream&) const;
  78.     friend ostream& operator<< (ostream& out, const Patron& pn) {
  79.          return pn.printPatron(out);
  80.  }
  81.     friend void checkOutBook();
  82.     friend void returnBook();
  83.  friend class Book;
  84. };

  85. list<Author> catalog['Z'+1];
  86. list<Patron> people['Z'+1];

  87. ostream& Author::printAuthor(ostream& out) const {
  88.  out << name << endl;
  89.  list<Book>::const_iterator ref = books.begin();
  90.     for ( ; ref != books.end(); ref++)
  91.         out << *ref; // overloaded <<
  92.  return out;
  93. }

  94. ostream& Book::printBook(ostream& out) const {
  95.     out << "    * " << title;
  96.     if (patron != 0)
  97.         out << " - checked out to " << patron->name; // overloaded <<
  98.     out << endl;
  99.     return out;
  100. }

  101. ostream& Patron::printPatron(ostream& out) const {
  102.     out << name;
  103.     if (!books.empty()) {
  104.         out << " has the following books:\n";
  105.         list<CheckedOutBook>::const_iterator bk = books.begin();
  106.         for ( ; bk != books.end(); bk++)
  107.             out << "    * " << bk->author->name << ", "
  108.                 << bk->book->title << endl;
  109.     }
  110.     else out << " has no books\n";
  111.     return out;
  112. }

  113. template<class T>
  114. ostream& operator<< (ostream& out, const list<T>& lst) {
  115.  list<T>::const_iterator ref;
  116.     for (ref = lst.begin(); ref != lst.end(); ref++)
  117.         out << *ref; // overloaded <<
  118.     return out;
  119. }

  120. char* getString(char *msg) {
  121.     char s[82], i, *destin;
  122.     cout << msg;
  123.     cin.get(s,80);
  124.     while (cin.get(s[81]) && s[81] != '\n');  // discard overflowing
  125.     destin = new char[strlen(s)+1];           // characters;
  126.     for (i = 0; destin[i] = toupper(s[i]); i++);
  127.     return destin;
  128. }

  129. void status() {
  130.     register int i;
  131.     cout << "Library has the following books:\n\n";
  132.     for (i = 'A'; i <= 'Z'; i++)
  133.         if (!catalog[i].empty())
  134.             cout << catalog[i];
  135.     cout << "\nThe following people are using the library:\n\n";
  136.     for (i = 'A'; i <= 'Z'; i++)
  137.         if (!people[i].empty())
  138.             cout << people[i];
  139. }

  140. void includeBook() {
  141.     Author newAuthor;
  142.     Book newBook;
  143.     newAuthor.name = getString("Enter author's name: ");
  144.     newBook.title  = getString("Enter the title of the book: ");
  145.     list<Author>::iterator oldAuthor =
  146.          find(catalog[newAuthor.name[0]].begin(),
  147.                      catalog[newAuthor.name[0]].end(),newAuthor);
  148.     if (oldAuthor == catalog[newAuthor.name[0]].end()) {
  149.          newAuthor.books.push_front(newBook);
  150.          catalog[newAuthor.name[0]].push_front(newAuthor);
  151.     }
  152.     else (*oldAuthor).books.push_front(newBook);
  153. }

  154. void checkOutBook() {
  155.     Patron patron;
  156.     Author author;
  157.     Book book;
  158.     list<Author>::iterator authorRef;
  159.     list<Book>::iterator bookRef;
  160.     patron.name = getString("Enter patron's name: ");
  161.     while (true) {
  162.         author.name = getString("Enter author's name: ");
  163.         authorRef = find(catalog[author.name[0]].begin(),
  164.                          catalog[author.name[0]].end(),author);
  165.        if (authorRef == catalog[author.name[0]].end())
  166.              cout << "Misspelled author's name\n";
  167.         else break;
  168.     }
  169.     while (true) {
  170.         book.title = getString("Enter the title of the book: ");
  171.         bookRef = find((*authorRef).books.begin(),
  172.                        (*authorRef).books.end(),book);
  173.         if (bookRef == (*authorRef).books.end())
  174.              cout << "Misspelled title\n";
  175.         else break;
  176.     }
  177.     list<Patron>::iterator patronRef;
  178.     patronRef = find(people[patron.name[0]].begin(),
  179.                      people[patron.name[0]].end(),patron);
  180.     CheckedOutBook checkedOutBook(authorRef,bookRef);
  181.     if (patronRef == people[patron.name[0]].end()) { // a new patron
  182.          patron.books.push_front(checkedOutBook);    // in the library;
  183.          people[patron.name[0]].push_front(patron);
  184.          (*bookRef).patron = &*people[patron.name[0]].begin();
  185.     }
  186.     else {
  187.          (*patronRef).books.push_front(checkedOutBook);
  188.          (*bookRef).patron = &*patronRef;
  189.     }
  190. }

  191. void returnBook() {
  192.     Patron patron;
  193.     Book book;
  194.     Author author;
  195.     list<Patron>::iterator patronRef;
  196.     list<Book>::iterator bookRef;
  197.     list<Author>::iterator authorRef;
  198.     while (true) {
  199.         patron.name = getString("Enter patron's name: ");
  200.         patronRef = find(people[patron.name[0]].begin(),
  201.                          people[patron.name[0]].end(),patron);
  202.         if (patronRef == people[patron.name[0]].end())
  203.              cout << "Patron's name misspelled\n";
  204.         else break;
  205.     }
  206.     while (true) {
  207.         author.name = getString("Enter author's name: ");
  208.         authorRef = find(catalog[author.name[0]].begin(),
  209.                          catalog[author.name[0]].end(),author);
  210.         if (authorRef == catalog[author.name[0]].end())
  211.              cout << "Misspelled author's name\n";
  212.         else break;
  213.     }
  214.     while (true) {
  215.         book.title = getString("Enter the title of the book: ");
  216.         bookRef = find((*authorRef).books.begin(),
  217.                        (*authorRef).books.end(),book);
  218.         if (bookRef == (*authorRef).books.end())
  219.              cout << "Misspelled title\n";
  220.         else break;
  221.     }
  222.     CheckedOutBook checkedOutBook(authorRef,bookRef);
  223.     (*bookRef).patron = 0;
  224.     (*patronRef).books.remove(checkedOutBook);
  225. }

  226. int menu() {
  227.     int option;
  228.     cout << "\nEnter one of the following options:\n"
  229.          << "1. Include a book in the catalog\n2. Check out a book\n"
  230.          << "3. Return a book\n4. Status\n5. Exit\n"
  231.          << "Your option? ";
  232.     cin >> option;
  233.     cin.get();         // discard '\n';
  234.     return option;
  235. }

  236. int main() {
  237.     while (true)
  238.         switch (menu()) {
  239.             case 1: includeBook();  break;
  240.             case 2: checkOutBook(); break;
  241.             case 3: returnBook();   break;
  242.             case 4: status();       break;
  243.             case 5: return 0;
  244.             default: cout << "Wrong option, try again: ";
  245.         }
  246.     return 0;
  247. }

复制代码

[ 本帖最后由 nmglzlong 于 2008-7-24 16:43 编辑 ]

main.rar

1.85 KB, 下载次数: 40

论坛徽章:
0
2 [报告]
发表于 2008-07-24 16:18 |只看该作者
程序调用的库是不一样的。。。

论坛徽章:
0
3 [报告]
发表于 2008-07-24 16:26 |只看该作者
试试这样
for (list<T>::const_iterator ref = lst.begin(); ref != lst.end(); ref++)

论坛徽章:
0
4 [报告]
发表于 2008-07-24 16:28 |只看该作者
应该是代码有点问题

论坛徽章:
0
5 [报告]
发表于 2008-07-24 16:38 |只看该作者
回楼上的。。。代码问题的话,在VC下难道就能通过吗?还有。我知道调的库不同,但是,要怎么样改呢?

linux下调试程序是刚接触。。。真的是不怎么会啊。。而且感觉标准好多都不一样?。。

论坛徽章:
0
6 [报告]
发表于 2008-07-24 16:46 |只看该作者
list<T>::const_iterator ref;
改成 typename list<T>::iterator ref; 试一试

论坛徽章:
0
7 [报告]
发表于 2008-07-24 17:05 |只看该作者
3楼的。。不是这个问题,这些我试过,但是不行的。

论坛徽章:
0
8 [报告]
发表于 2008-07-24 17:06 |只看该作者
6楼的好棒哦。能讲讲是为什么吗?

论坛徽章:
0
9 [报告]
发表于 2008-07-24 17:10 |只看该作者

  1.     CheckedOutBook(list<Author>::iterator ar = 0,
  2.                             list<Book>::iterator bk = 0)
复制代码


这一行能过???

论坛徽章:
0
10 [报告]
发表于 2008-07-24 17:12 |只看该作者
把0去了就能过了。。现在是在win下用dev c++调试的,。。但是最主要的问题应该是解决了,但是不知道是什么原因啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP