读书人

请问一个关于重载运算符的模板函数的有

发布时间: 2012-02-17 17:50:42 作者: rapoo

请教一个关于重载运算符的模板函数的问题
这是 出错提示 :[root@localhost library]# g++ -o library library.cpp
library.cpp: In function `std::ostream& operator < <(std::ostream&, const std::list <T, std::allocator <_CharT> > &) ':
library.cpp:125: error: expected `; ' before "ref "
library.cpp:126: error: `ref ' undeclared (first use this function)
library.cpp:126: error: (Each undeclared identifier is reported only once for each function it appears in.)
library.cpp: In function `std::ostream& operator < <(std::ostream&, const std::list <T, std::allocator <_CharT> > &) [with T = Author] ':
library.cpp:146: instantiated from here
library.cpp:125: error: dependent-name ` std::list <T,std::allocator <_CharT> > ::iterator ' is parsed as a non-type, but instantiation yields a type
library.cpp:125: note: say `typename std::list <T,std::allocator <_CharT> > ::iterator ' if a type is meant
library.cpp: In function `std::ostream& operator < <(std::ostream&, const std::list <T, std::allocator <_CharT> > &) [with T = Patron] ':
library.cpp:150: instantiated from here
library.cpp:125: error: dependent-name ` std::list <T,std::allocator <_CharT> > ::iterator ' is parsed as a non-type, but instantiation yields a type
library.cpp:125: note: say `typename std::list <T,std::allocator <_CharT> > ::iterator ' if a type is meant
下面是我的源代码:
#include <iostream>

#include <string>

#include <list>

#include <algorithm>


using namespace std;


class Patron; // forward declaration;


class Book {

public:

Book() {

patron = 0;

}

bool operator== (const Book& bk) const {

return strcmp(title,bk.title) == 0;

}

private:

char *title;

Patron *patron;

ostream& printBook(ostream&) const;

friend ostream& operator < < (ostream& out, const Book& bk) {

return bk.printBook(out);

}

friend class CheckedOutBook;


friend class Patron;

friend void includeBook();

friend void checkOutBook();

friend void returnBook();

};


class Author {

public:

Author() {

}

bool operator== (const Author& ar) const {

return strcmp(name,ar.name) == 0;

}

private:

char *name;

list <Book> books;

ostream& printAuthor(ostream&) const;

friend ostream& operator < < (ostream& out,const Author& ar) {

return ar.printAuthor(out);

}

friend void includeBook();

friend void checkOutBook();

friend void returnBook();

friend class CheckedOutBook;

friend class Patron;

};


class CheckedOutBook {

public:

CheckedOutBook(list <Author> ::iterator ar = 0,

list <Book> ::iterator bk = 0) {

author = ar;

book = bk;

}

bool operator== (const CheckedOutBook& bk) const {

return strcmp(author-> name,bk.author-> name) == 0 &&

strcmp(book-> title,bk.book-> title) == 0;

}

private:

list <Author> ::iterator author;

list <Book> ::iterator book;

friend void checkOutBook();

friend void returnBook();

friend class Patron;

};


class Patron {

public:

Patron() {

}

bool operator== (const Patron& pn) const {

return strcmp(name,pn.name) == 0;

}

private:

char *name;

list <CheckedOutBook> books;

ostream& printPatron(ostream&) const;



friend ostream& operator < < (ostream& out, const Patron& pn) {

return pn.printPatron(out);

}

friend void checkOutBook();

friend void returnBook();

friend class Book;

};


list <Author> catalog[ 'Z '+1];

list <Patron> people[ 'Z '+1];


ostream& Author::printAuthor(ostream& out) const {

out < < name < < endl;

list <Book> ::const_iterator ref = books.begin();

for ( ; ref != books.end(); ref++)

out < < *ref; // overloaded < <

return out;

}


ostream& Book::printBook(ostream& out) const {

out < < " * " < < title;

if (patron != 0)

out < < " - checked out to " < < patron-> name; // overloaded < <

out < < endl;

return out;

}


ostream& Patron::printPatron(ostream& out) const {

out < < name;

if (!books.empty()) {

out < < " has the following books:\n ";

list <CheckedOutBook> ::const_iterator bk = books.begin();

for ( ; bk != books.end(); bk++)

out < < " * " < < bk-> author-> name < < ", "

< < bk-> book-> title < < endl;

}

else out < < " has no books\n ";

return out;

}


template <typename T>

ostream& operator < < (ostream& out, const list <T> & lst) {

list <T> ::iterator ref;

for (ref = lst.begin(); ref != lst.end(); ref++)

out < < *ref; // overloaded < <



return out;

}


char* getString(char *msg) {

char s[82], i, *destin;

cout < < msg;

cin.get(s,80);

while (cin.get(s[81]) && s[81] != '\n '); // discard overflowing

destin = new char[strlen(s)+1]; // characters;

for (i = 0; destin[i] = toupper(s[i]); i++);

return destin;

}


void status() {

register int i;

cout < < "Library has the following books:\n\n ";

for (i = 'A '; i <= 'Z '; i++)

if (!catalog[i].empty())

cout < < catalog[i];

cout < < "\nThe following people are using the library:\n\n ";

for (i = 'A '; i <= 'Z '; i++)

if (!people[i].empty())

cout < < people[i];

}



[解决办法]
typename list <T> ::iterator ref;

[解决办法]
template <typename T>
ostream& operator < < (ostream& out, const list <T> & lst) {
list <T> ::iterator ref;

改为:

template <typename T>
ostream& operator < < (ostream& out, const list <T> & lst) {
list <T> ::const_iterator ref;

读书人网 >C++

热点推荐