QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 734|回复: 2

一个例子程序,关于重载<<,>>麻烦各位看看贝

[复制链接]
发表于 2005-6-13 10:37:04 | 显示全部楼层 |阅读模式
试了好几次,还是不知道错在哪里,以下是部分代码:
#include <iostream.h>
#include <strstream.h>
#include <stdio.h>
//------
class intarray{
  enum {sz=5};
  int i[sz];
public:
  ......
  friend ostream&
    operator<<(ostream& os,const intarray& ia);
  friend istream&
    operator>>(istream& is,const intarray& ia);
};

ostream& operator<<(ostream& os,const intarray& ia){
     ......
};
istream& operator>>(istream& is,const intarray& ia){
      ......
          };

main()
{
  istrstream input("12,23,34,45,56");
  intarray I;
  input >> I;
  I[4]=-1;
  cout<<I;
}

编译显示(好像挺多,其实关键的不多):
[zhu@nobody c++]$ g++ opr1.cpp -o opr1
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
                 from opr1.cpp:3:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
opr1.cpp: In function `std::istream& operator>>(std::istream&, const
   intarray&)':
opr1.cpp:40: ambiguous overload for `std::basic_istream<char,
   std::char_traits<char> >& >> const int&' operator
/usr/include/c++/3.2.2/bits/istream.tcc:83: candidates are:
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
   _Traits>:perator>>(std::basic_istream<_CharT,
   _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char,
   _Traits = std::char_traits<char>] <near match>
/usr/include/c++/3.2.2/bits/istream.tcc:92:
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
   _Traits>:perator>>(std::basic_ios<_CharT,
   _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
   = std::char_traits<char>] <near match>
/usr/include/c++/3.2.2/bits/istream.tcc:101:
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
   _Traits>:perator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
   _Traits = std::char_traits<char>] <near match>
/usr/include/c++/3.2.2/bits/istream.tcc:483:
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
   _Traits>:perator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT =
   char, _Traits = std::char_traits<char>] <near match>
/usr/include/c++/3.2.2/istream:255:                 std::basic_istream<char,
   _Traits>& std:perator>>(std::basic_istream<char, _Traits>&, signed char*)
   [with _Traits = std::char_traits<char>] <near match>
/usr/include/c++/3.2.2/istream:250:                 std::basic_istream<char,
   _Traits>& std:perator>>(std::basic_istream<char, _Traits>&, unsigned
   char*) [with _Traits = std::char_traits<char>] <near match>
[zhu@nobody c++]$
发表于 2005-6-13 12:42:00 | 显示全部楼层
[code:1]
#include <iostream>        // #include <iostream.h>
#include <sstream>        // #include <strstream.h>
                                 // #include <stdio.h>       
//------
class intarray{
    enum {sz=5};
    int i[sz];
    public:
    //......
    friend std::ostream&
        operator<<(std::ostream& os,const intarray& ia);
    friend std::istream&
        operator>>(std::istream& is,const intarray& ia);
};

std::ostream& operator<<(std::ostream& os,const intarray& ia){
    //......
    return os;
};
std::istream& operator>>(std::istream& is,const intarray& ia){
    //......
    return is;
};

int main()
{
    std::istringstream input("12,23,34,45,56");
    intarray I;
    input >> I;
//    I[4]=-1;
    std::cout << I;

    return 0;
}

[/code:1]


gcc version 3.3.4
回复

使用道具 举报

 楼主| 发表于 2005-6-13 16:26:02 | 显示全部楼层
我的是gcc-3.2-9,编译不通过。
回复

使用道具 举报

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

本版积分规则

GMT+8, 2024-11-5 21:41 , Processed in 0.059745 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

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