Hi all here is my pair.h file which when I run using g++ on linux I am getting error like

"pair.h:39: error: expected constructor, destructor, or type conversion before ‘ostream’"

any help would be greatly appreciated!



template <class T1, class T2>
class pair2 {

        T1 first_;
        T2 second_;
        //friend ostream& operator << <T1,T2>(ostream& os, pair2<T1,T2> const& p);
//      friend istream& operator >> <T1,T2>(istream& is, pair2<T>& p);
        pair2(const T1 &a, const T2 &b){
        first_= a;
        second_= b;

        void set_first (const T1& a) {first_= a;}
        void set_second(const T2& b) {second_= b;}
        void swap(){
                T1 temp = second_;
                second_= static_cast<T2>(first_);
                first_= static_cast<T1>(temp);

        T1 first()const {return first_;}
        T2 second()const {return second_;}

        bool operator==(pair2 const& c)const{
                bool bool1;
                bool1= ((this->first_ == c.first())&&(this->second_==c.second()));
                return bool1;


template <class T1, class T2> pair2
 ostream& operator<<(ostream& os,  pair2 <T1,T2> const& c){
 os<<c.first()<<" "<<c.second()<<endl;
return os;

Recommended Answers

All 5 Replies


Seems like you would like to overload << operator for your generic class pair2.
You have not declared the overloaded operator function in your class and then you are defining it outside the class.

Either define it inside class or declare it inside class and then define it outside.

Actually I want to be a global template function, which I think you don't need to do it!

Uncomment the friend function declaration

//friend ostream& operator << <T1,T2>(ostream& os, pair2<T1,T2> const& p);

Then edit the function definition for overloaded operator << outside the class to match the friend function declaration. It should work ..

Thanks it worked, if I use include"iostream" and using namespace std;
I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.