Creating an ostream with Pair struct

I have created a Pair struct to hold pairs but right now it doesn't like the ostream I have created. Specifically, it doesn't like the amount of arguments it takes. The error I'm getting is: error: 'std::ostream& Pair<T1, T2>::operator<<(std::ostream&, const Pair<T1, T2>&)' must take exactly one argument


How do I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  template <typename T1, typename T2>
struct Pair;

template <typename T1, typename T2>
std::ostream &operator <<(std::ostream &os, const Pair<T1, T2> &value);

template <typename T1,typename T2>
struct Pair {
		std::ostream &operator << <>(std::ostream &os, const Pair<T1, T2> &value) {
            os << '(' << value.first << ',' << value.second << ')';
            return os;
        }
		Pair(const T1 &first, const T2 &second) {
		this->first = first;
		this->second = second;
	}
	T1 first;
	T2 second;
};
The code posted is basically a syntax error: "operator << <>" looks like the syntax for a template specialization of a class member function, but it's missing some keywords which depend on what you're trying to do. Based on the rest of it, what you're trying to is to friend a specialization of the non-member template you've declared at lines 4-5: you're missing the keyword "friend"

1
2
3
template <typename T1,typename T2>
struct Pair {
        friend std::ostream &operator << <>(std::ostream &os, const Pair<T1, T2> &value) {


Last edited on
There is already a pair class in <utility> by the way.
Topic archived. No new replies allowed.