Here is my code:
#include <iostream>
#include <cstddef>
class alloc
{
};
template <class T, class Alloc = alloc, size_t BufSiz = 0>
class deque
{
public:
deque() { std::cout << "deque" << std::endl; }
};
template <class T, class Sequence = deque<T> >
class stack
{
public:
stack() { std::cout << "stack" << std::endl; }
private:
Sequence c;
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
};
template <class T, class Sequence>
bool operator== (const stack<T, Sequence>&x, const stack<T, Sequence>&y)
{
return std::cout << "opertor == " << std::endl;
}
template <class T, class Sequence>
bool operator < (const stack<T, Sequence> &x, const stack<T, Sequence> &y)
{
return std::cout << "operator <" << std::endl;
}
int main()
{
stack<int> x; // deque stack
stack<int> y; // deque stack
std::cout << (x == y) << std::endl; // operator == 1
std::cout << (x < y) << std::endl; // operator < 1
}
I just thought a simple <> notation tell the compiler my function is function template. But I get error with two line:friends can only be classes or functions
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
How can I do to solve it.
Just use this syntax:
template<typename T1, typename Sequence1>
friend bool operator== (const stack<T1, Sequence1>&, const stack<T1, Sequence> &);
template<typename T1, typename Sequence1>
friend bool operator< (const stack<T1, Sequence1>&, const stack<T1, Sequence>&);
You nees the first template parameter to be different that T and the second to be different than Sequence, or you will be shadowing the class' template parameters.