LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 816|回复: 2

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

[复制链接]
发表于 2007-4-13 11:23:31 | 显示全部楼层 |阅读模式
编译时出错为 :[root@localhost library]# g++ -o library library.cpp
library.cpp: In function `std:stream& operator<<(std:stream&, 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:stream& operator<<(std:stream&, 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:stream& operator<<(std:stream&, 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
 楼主| 发表于 2007-4-13 11:27:21 | 显示全部楼层
部分代码
template<typename T>
ostream& operator<< (ostream& out, const list<T>& lst) {
    ist<T>::iterator ref;
    for (ref = lst.begin(); ref != lst.end(); ref++)
    out << *ref; // overloaded <<
    return out;
   }
list<Author> catalog['Z'+1];
list<atron> people['Z'+1];
void status() {
    register int i;
    cout << "Library has the following books:\n\n";
    for (i = 'A'; i <= 'Z'; i++)
        if (!catalog.empty())
            cout << catalog;
    cout << "\nThe following people are using the library:\n\n";
    for (i = 'A'; i <= 'Z'; i++)
        if (!people.empty())
            cout << people;
}
回复 支持 反对

使用道具 举报

发表于 2007-4-13 15:44:14 | 显示全部楼层
  1. library.cpp:125: error: dependent-name ` std::list<T,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type
  2. library.cpp:125: note: say `typename std::list<T,std::allocator<_CharT> >::iterator' if a type is meant
复制代码
这个提示很明确,如果不清楚请 google 一下 typename
回复 支持 反对

使用道具 举报

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

本版积分规则

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