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;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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 ..

Edited 6 Years Ago by vidit_X: n/a

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

This question has already been answered. Start a new discussion instead.