注册 登录
编程论坛 C++教室

求助~~~~~~~~~~~~~哪里有错误,总是过不去

taky123 发布于 2008-01-08 12:14, 1458 次点击
#include <iostream>
#include <string>
using namespace std;
template<class T>
class ff
{
    public:
        ff(T a=T() ):value(a) {}
        void setValue(T k) {value=k;}
        friend ostream& operator << (ostream &,ff<T> &);
    private:
        T value;
};

template<class T>
ostream& operator << (ostream& output,ff<T>& cc)
{
    output << "the value is " << cc.value() << endl;
    return output;
}

int main()
{
    ff<int> aa;
    cout << aa ;
    system("pause");
    return 0;
}
我把它化的很简了,但是重载流和模板联系的时候还是有错,大家帮我看看,先谢了
14 回复
#2
zjl1382008-01-08 13:15
你用的编译器是VC6。0吗?
#3
zjl1382008-01-08 13:43
output << "the value is " << cc.value() << endl;
不明白你的cc.value() 是什么,应该是cc.value这样吧!
还有如果你用的编译器是VC6.0的话,调用友元会出错!!!但改用#include<iostream.h>这个头文件又没错,这点我也不大清楚,还是等大牛们来回答吧!!!
以上薄见,勿笑!!!
#4
中学者2008-01-08 14:01
应该是编译器的问题.....搞不懂VC6.0连友元都不能访问私有成员是为啥~
#5
linsua2008-01-08 14:08
你把  friend ostream& operator << (ostream &,ff<T> &);
改成:template<class U>
            friend ostream& operator << (ostream &,ff<U> &);
就好了,原因看Hurb Sutter的三卷本
#6
taky1232008-01-08 15:23
谢谢楼上的,高手啊~~~
我会去看那本书的
#7
中学者2008-01-08 16:24
有谁告诉我非模板的友元也不能访问私有成员是为何??
#8
sunkaidong2008-01-08 17:08
#include <iostream.h>
#include <string.h>
//using namespace std;
template<class T>
class ff
{
    public:
        ff(T k){ setValue(k);}
        void setValue(T k) {value=k;}
        friend ostream& operator << (ostream &,ff<T> &);
    private:
        T value;
};

template<class T>
ostream  &operator<< (ostream& output,ff<T>& cc)
{
    output << "the value is " << cc.value<< endl;
    return output;
}

int main()
{  
   
    ff<int> aa(8);
    cout << aa ;
    //system("pause");
    return 0;
}
你得程序我有点不懂.
ff(T a=T() ):value(a) {}可以告诉我是什么意思吗?????
#9
sunkaidong2008-01-08 17:09
你得程序我有点不懂.ff(T a=T() ):value(a) {}什么意思啊?
这是我的:#include <iostream.h>
#include <string.h>
//using namespace std;
template<class T>
class ff
{
    public:
        ff(T k){ setValue(k);}
        void setValue(T k) {value=k;}
        friend ostream& operator << (ostream &,ff<T> &);
    private:
        T value;
};

template<class T>
ostream  &operator<< (ostream& output,ff<T>& cc)
{
    output << "the value is " << cc.value<< endl;
    return output;
}

int main()
{  
   
    ff<int> aa(8);
    cout << aa ;
    //system("pause");
    return 0;
}
#10
taky1232008-01-08 18:12
回复 8# 的帖子
ff(T a=T() ):value(a) {}是构造函数表,用来初始化类用的
还可以写成 ff( T a=T() ) {value=a;}     
T()是表示初值为0或空

还有,你的程序运行不了啊,我问的问题你看下,好像你也没注意那~
#11
sunkaidong2008-01-08 21:22
VC6.0能运行.
#12
火乍弓单2008-01-08 22:21
#include <iostream.h>
#include <string.h
using namespace std;
template<class T>
class ff
{
    public:
        ff(T a=T() ):value(a) {}
        void setValue(T k) {value=k;}
        friend ostream& operator << (ostream &,ff<T> &);
    private:
        T value;
};

template<class T>
ostream& operator << (ostream& output,ff<T>& cc)
{
    output << "the value is " << cc.value() << endl;
    return output;
}

int main()
{
    ff<int> aa;
    cout << aa ;
    system("pause");
    return 0;
}
#13
火乍弓单2008-01-08 22:23
呵呵,我新手
#14
xiangz90252008-01-09 23:21
system这个关键字怎么在我机器上也不能用阿!
#15
linsua2008-01-10 00:39
原帖由 [bold][underline]xiangz9025[/underline][/bold] 于 2008-1-9 23:21 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1176483&ptid=196461][/url]
system这个关键字怎么在我机器上也不能用阿!


毛个关键字,
system是函数
#include<cstdlib>
1