请教一下C++ STL set问题,模板类,重载<<输出
定义一个类型Edgetemplate<classT>classEdge{public:friendostream&operator<<(ostream&,constEdg...
定义一个类型Edge
template <class T>
class Edge
{
public:
friend ostream& operator << (ostream&,const Edge<T>&);
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 <class T>
ostream& operator << (ostream &os,Edge<T> &edge)
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
template <class T>
struct strLess
{
bool operator() (Edge<T> e1, Edge<T> e2)
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
set <Edge<int>,stlLess<int>> s;
set<Edge<T>,strLess<T>>::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " ";
}
rerun 0;
}
为什么上面出错,无法解析的错误,是我重载的不对吗?
为什么重载时不能访问私有成员,我不是申明了友元吗?
问题困扰了很久,哪位大侠帮我解决了,感激不尽,另有50积分相送。 展开
template <class T>
class Edge
{
public:
friend ostream& operator << (ostream&,const Edge<T>&);
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 <class T>
ostream& operator << (ostream &os,Edge<T> &edge)
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
template <class T>
struct strLess
{
bool operator() (Edge<T> e1, Edge<T> e2)
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
set <Edge<int>,stlLess<int>> s;
set<Edge<T>,strLess<T>>::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " ";
}
rerun 0;
}
为什么上面出错,无法解析的错误,是我重载的不对吗?
为什么重载时不能访问私有成员,我不是申明了友元吗?
问题困扰了很久,哪位大侠帮我解决了,感激不尽,另有50积分相送。 展开
2013-09-02
展开全部
友元函数是一个模板函数,声明的时候要加上template <class T>。
成员函数有this指针,而友元函数没有this指针。所以类声明中的T对成员函数是可见的,对友元函数不可见。
成员函数有this指针,而友元函数没有this指针。所以类声明中的T对成员函数是可见的,对友元函数不可见。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
吓尿, 没初始化set就直接输出. 哪抄来的代码.
帮你修正了代码的错误, 重载没问题
#include <iostream>
#include <set>
using namespace std ;
template <class T>class Edge
{
private:
int v1;
int v2;
T data;
public:
template <class T>
friend ostream& operator << (ostream& os,const Edge<T>& 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 <class T>
struct strLess
{
bool operator() (Edge<T> e1, Edge<T> e2)
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
Edge< int > e( 0, 0, 1 ) ;
Edge< int> e1( 0, 0, 2 ) ;
Edge<int> e2( 0, 0, 3 ) ;
set <Edge<int>,strLess<int>> s;
s.insert( e ) ;
s. insert( e2 ) ;
s. insert( e1 ) ;
set<Edge<int>,strLess<int>>::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " " << endl ;;
}
return 0;
}
帮你修正了代码的错误, 重载没问题
#include <iostream>
#include <set>
using namespace std ;
template <class T>class Edge
{
private:
int v1;
int v2;
T data;
public:
template <class T>
friend ostream& operator << (ostream& os,const Edge<T>& 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 <class T>
struct strLess
{
bool operator() (Edge<T> e1, Edge<T> e2)
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
Edge< int > e( 0, 0, 1 ) ;
Edge< int> e1( 0, 0, 2 ) ;
Edge<int> e2( 0, 0, 3 ) ;
set <Edge<int>,strLess<int>> s;
s.insert( e ) ;
s. insert( e2 ) ;
s. insert( e1 ) ;
set<Edge<int>,strLess<int>>::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " " << endl ;;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
基本的模板语法很多地方都有问题,基本的东西都没弄清楚,基础不扎实啊,
#include <iostream>
#include <set>
using namespace std;
template <class T>
class Edge
{
public:
template<typename Val>
friend ostream& operator << (ostream&, const Edge<Val>&);
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 <class T>
ostream& operator << (ostream &os, const Edge<T> &edge)
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
template <class T>
struct strLess : public binary_function< Edge<T>, Edge<T>, bool>
{
bool operator() (const Edge<T>& e1, const Edge<T>& e2) const
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
set <Edge<int>, strLess<int> > s;
set< Edge<int>, strLess<int> >::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " ";
}
return 0;
}
#include <iostream>
#include <set>
using namespace std;
template <class T>
class Edge
{
public:
template<typename Val>
friend ostream& operator << (ostream&, const Edge<Val>&);
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 <class T>
ostream& operator << (ostream &os, const Edge<T> &edge)
{
os << edge.v1 << " " << edge.v2 << " " << edge.data;
return os;
}
template <class T>
struct strLess : public binary_function< Edge<T>, Edge<T>, bool>
{
bool operator() (const Edge<T>& e1, const Edge<T>& e2) const
{
return (e1.getData()-e2.getData())<0;
}
};
int main()
{
set <Edge<int>, strLess<int> > s;
set< Edge<int>, strLess<int> >::iterator iter;
for (iter=s.begin();iter!=s.end();iter++)
{
cout << *iter << " ";
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询