友元函数是一个模板函数,声明的时候要加上template
成员函数有this指针,而友元函数没有this指针。所以类声明中的T对成员函数是可见的,对友元函数不可见。
吓尿, 没初始化set就直接输出. 哪抄来的代码.
帮你修正了代码的错误, 重载没问题
#include
#include
using namespace std ;
template
{
private:
int v1;
int v2;
T data;
public:
template
friend ostream& operator << (ostream& os,const Edge
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
Edge(int v1,int v2,T data)
{
this->data = data;
this->v1 = v1;
this->v2 = v2;
}
T getData()
{
return this->data;
}
};
template
struct strLess
{
bool operator() (Edge
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
Edge< int > e( 0, 0, 1 ) ;
Edge< int> e1( 0, 0, 2 ) ;
Edge
set
s.insert( e ) ;
s. insert( e2 ) ;
s. insert( e1 ) ;
set
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " " << endl ;;
}
return 0;
}
基本的模板语法很多地方都有问题,基本的东西都没弄清楚,基础不扎实啊,
#include
#include
using namespace std;
template
class Edge
{
public:
template
friend ostream& operator << (ostream&, const Edge
Edge(int v1,int v2,T data)
{
this->data = data;
this->v1 = v1;
this->v2 = v2;
}
T getData()
{
return this->data;
}
private:
int v1;
int v2;
T data;
};
template
ostream& operator << (ostream &os, const Edge
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
template
struct strLess : public binary_function< Edge
{
bool operator() (const Edge
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
set
set< Edge
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " ";
}
return 0;
}