我用VC创建了一个Rantional类,想再加上对输出运算符<<的重载
但是编译后说friend函数不能放在Rantional.h里,
我是新手,不知道怎么做,请大家指教。
先谢谢了!!>_<
friend ostream & operator <<(ostream &stream, Rational &s)
{
stream << s.d << s.n << endl;
return stream;
}

解决方案 »

  1.   

    在.h文件中写下如下代码
    __inline ostream & operator <<(ostream &stream, Rational &s)
    {
    stream << s.d << s.n << endl;
    return stream;
    }类声明中写:friend __inline ostream & operator <<(ostream &stream, Rational &s);
      

  2.   

    还是出错/*我是用VC工作区的CLASSVIEW右键->new class ...创建的类,
    成员是用add func/var ...添加的。*/谁能解决这个问题吗??
      

  3.   

    不要这样加,会出错,建议你找个c++的数据结构的书看看,源代码一大堆。把我自己写的CString代码贴一下(随便写的,有很多错误,不过语法倒还比较正确),看对你有帮助没。
    string.h文件
    class CString:public Node
    {
    public:
    void MakeUpper();
    void AllocCopy(LPTSTR str,int nLen,int nIndex);
    CString& Right(int t);
    int Find(CString& pat) const;
    int Find(LPCTSTR pat) const;
    bool AllocSize(int num);
    CString& Mid(int Pos,int Num);
    void StrCopy(const CString& obj);
    bool IsEmpty();
    bool Empty();
    size_t GetLength();
    CString& Left(int t);
    CString& operator += (TCHAR ch);
    CString& operator += (const CString& str);
    CString& operator += (const LPTSTR str);
    TCHAR& operator[] (int number);
    const LPTSTR GetStr();
    CString():m_IsCopy(FALSE){m_pStr=NULL;m_len=0;};
    CString(CString& str);
    CString& operator= (const LPTSTR str);
    CString& operator= (const CString& str);
    CString(LPCTSTR str):m_IsCopy(FALSE){
    m_len=_tcslen(str);
    m_pStr=new TCHAR[m_len+1];
    _tcscpy(m_pStr,str);
    if(_tcsstr(m_pStr,_T("\0"))==NULL)
    m_pStr[m_len]=_T('\0');
    }
    LPCTSTR operator ()(const CString& str);
    virtual ~CString(){if(m_pStr!=NULL)delete m_pStr;}
    LPTSTR m_pStr;
    LPTSTR m_pData; private:
    bool m_IsCopy;
    size_t m_len;
    };
    string.cpp文件
    CString& CString::operator += (TCHAR ch)
    {
    LPTSTR temp=m_pStr;
    m_len+=1;
    m_pStr=new TCHAR[m_len+1];
    if(!m_pStr){CFMSG("内存分配出错!");exit(1);}
    _tcscpy(m_pStr,temp);
    _tcscat(m_pStr,&ch);
    delete []temp;
    return *this;
    }CString& CString::operator += (const CString& str)
    {
    (*this)+=str.m_pStr;
    return *this;
    }CString& CString::operator += (const LPTSTR str)
    {
    LPTSTR temp=m_pStr;
    m_len+=_tcslen(str);
    m_pStr=new TCHAR[m_len+1];
    if(!m_pStr){CFMSG("内存分配出错!");exit(1);}
    if(temp!=NULL)
    {
    _tcscpy(m_pStr,temp);
    _tcscat(m_pStr,str);
    }
    else
    {
    _tcscpy(m_pStr,str);
    }
    delete []temp;
    return *this;
    }
      

  4.   

    自己测试了一下,我怎么编译通过,而且运行也正确了呢?
    #include <iostream>
    using namespace std;
    class x
    {
    public:
    x(){z=35;};
    friend ostream & operator <<(ostream &stream, x &s);
    private:
    int z;
    };ostream & operator<<(ostream& stream,x &s)
    {
    stream<<s.z<<endl;
    return stream;
    }int main()
    {
    x s;
    cout<<s;
    return 0;
    }
      

  5.   

    同意楼上的其实你也可以这样
    在你的类中添加一成员函数Output()
    在这个函数中你进行一定格式的输出然后再添加一个全局函数
    // overload <<
    template <class T>
    ostream& operator<<(ostream& out, const CStack<T>& x)
    {
    x.Output(out);
    return out;
    }这样虽然比上面那种方法麻烦一点,不过条理更清晰一些。而且不用加friend--------------------
                 May you succeed!
                       ------------------------
      

  6.   

    我也有个问题,为什么我已经#include <iostream.h>
    但用
    iostream is1;
    却老是报错:不能访问保护成员,我看了一个iostream.h,
    里面的构造函数iostream()确实是保护成员。但是,我要如何才能定义自己的stream变量呀,
    因为我是要用stream来处理字符串,而不是用cin,cout进行屏幕和键盘输入输出呀。
      

  7.   

    哈,我自己知道啦。
    必须用ostrstream来创建对象。但奇怪的是ostrstream可以用<<。
    但istrstream对象却不能用>>操作符,郁闷呀:(