In The C++ Programming Language, Fourth Edition - chapter 23.4.7 Friends, I found following example (I have slightly modified it to show only relevant part):
template<typename T>
class Vector {
public:
friend Vector operator*<>(const Vector& v, int f);
^^ ~~~~ ?
};
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
return v;
}
I tried to compile it, but I get following error (clang):
main.cpp:8:20: error: friends can only be classes or functions
friend Vector operator*<>(const Vector& v, int f);
^
main.cpp:8:29: error: expected ';' at end of declaration list
friend Vector operator*<>(const Vector& v, int f);
^
;
2 errors generated.
Book explains that :
The <> after the name of the friend function is needed to make clear that the friend is a template function. Without the <>, a non template function would be assumed.
And that is all on this.
Without <>
this code compiles, but when operator* is used (ex.: Vector<int> v; v*12;
) then linker error appears:
main.cpp:(.text+0xb): undefined reference to `operator*(Vector<int> const&, int)'
So I assume that <>
is needed to tell compiler that function template for operator* should be generated each time Vector template is instantiated for given type.
But what am I doing wrong in the example from the book, and why?
Syntax of friend functions:class className{ // Other Declarations friend returnType functionName(arg list); }; As we can see above, the friend function should be declared inside the class whose private and protected members are to be accessed.
Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.
Syntax : class <class_name> { friend <return_type> <function_name>(argument/s); }; C++
What is the correct syntax of defining function template/template functions? Explanation: Starts with keyword template and then <class VAR>, then use VAR as type anywhere in the function below.
As the book said,
the
<>
after the name of the friend function is needed to make clear that the friend is a template function.
That means, the name should refer to a function template, which should be declared (as template) in advance. e.g.
// forward declaration of the class template
template<typename T>
class Vector;
// declaration of the function template
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f);
template<typename T>
class Vector {
public:
// friend declaration
friend Vector operator*<>(const Vector& v, int f);
};
// definition of the function template
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
return v;
}
In your case, you're declaring operator*
as a friend directly inside Vector
, without any previous declaration. Therefore the correct syntax is:
template<typename T>
class Vector {
public:
template<typename>
friend Vector operator*(const Vector& v, int f);
};
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
return v;
}
live example on wandbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With