friend operator<< in template class - c++

From what I know about friend functions this should work. I'm not sure what's going on.
In my code i define a class
template < class IType = unsigned int >
class BitArray {
...
friend ostream& operator<<(ostream&, const BitArray&);
friend istream& operator>>(istream&, BitArray&);
...
}
then later in the same header file
template < class IType >
ostream& operator<<(ostream& os, const BitArray<IType>& that)
{
...
}
template < class IType >
istream& operator>>(istream& is, BitArray<IType>& that)
{
...
}
and it's giving me
error LNK2019: unresolved external symbol
when I try to compile.
I've re-read and re-written this half a dozen times, and reviewed the use of the "friend" keyword, and can't find what's wrong.
does this implementation follow different rules because of the template
I also overwrote << and >> as shift operators, but again that shouldn't matter since they have different arguments;

With warning up, you have:
warning: friend declaration 'std::ostream& operator<<(std::ostream&, const BitArray<IType>&)' declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
So declare the template function before:
template <class IType> class BitArray;
template <class IType> std::ostream& operator<<(std::ostream&, const BitArray<IType>&);
template <class IType> std::istream& operator>>(std::istream&, BitArray<IType>&);
template < class IType = unsigned int >
class BitArray {
friend std::ostream& operator<< <>(std::ostream&, const BitArray&);
friend std::istream& operator>> <>(std::istream&, BitArray&);
};
template <class IType>
std::ostream& operator<<(std::ostream& os, const BitArray<IType>& b)
{
/* Your implementation */
}
Live example

I think you want this:
template < class IType = unsigned int >
class BitArray {
template<class> friend ostream& operator<<(ostream&, const BitArray&);
template<class> friend istream& operator>>(istream&, BitArray&);
};
Your BitArray declaration is telling the compiler to look for a non-templated operator<< and operator>>

Related

Friend template overloaded operator <<: unresolved external symbol

I'm having issues with the error
Error LNK2019 unresolved external symbol "class std::basic_ostream > & __cdecl cop4530::operator<<(class std::basic_ostream > &,class rob::Stack const &)" (??6rob##YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV12#ABV?$Stack#H#0##Z) referenced in function _main Project7 c:\Users\Robrik\documents\visual studio 2015\Projects\Project7\Project7\post.obj 1
Right now, all that post is doing is calling the operator<<
The declaration
namespace rob {
template < typename T> class Stack {
friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
void print(std::ostream& os, char ofc = ' ') const;
private:
std::vector<T> arr;
};
The definition
template < typename T>
inline std::ostream & rob::operator<<(std::ostream & os, const Stack<T>& a) {
return a.print(os, ' ');
}
template<typename T>
inline void rob::Stack<T>::print(std::ostream & os, char c) const
{
for (int i = 0; i != arr.size(); i++)
{
os << c << arr[i];
}
os << '\n';
}
They are located in a .h file and a .hpp respectively, I require that the operator is not a member function (for assignment).
The issue with the code sample;
template <typename T>
class Stack {
friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
void print(std::ostream& os, char ofc = ' ') const;
// ...
};
Is that the operator<< is being declared as a non-template function. For every type T used with Stack, there needs to be a non-template operator<<. For example, if there is a type Stack<int> declared, then there must be an operator implementation as follows;
std::ostream& operator<< (std::ostream& os, const Stack<int>& a) {/*...*/}
Since it is not implemented, the linker fails to find it and results in the error you get.
As a side note; gcc warns about this as follows
warning: friend declaration 'std::ostream& operator<<(...)' declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
This is probably not what is intended, that every instantiation has its own implementation.
To correct this, you can declare a template operator before the Stack type and then declare as a friend, an instantiation. The syntax looks a little awkward, but is looks as follows;
// forward declare the Stack
template <typename>
class Stack;
// forward declare the operator <<
template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&);
template <typename T>
class Stack {
friend std::ostream& operator<< <>(std::ostream& os, const Stack<T>& a);
// note the required <> ^^^^
void print(std::ostream& os, char ofc = ' ') const;
// ...
};
template <typename T>
std::ostream& operator<<(std::ostream&, const Stack<T>&)
{
// ... implement the operator
}
The above code limits the friendship of the operator to the corresponding instantiation of Stack, i.e. the operator<< <int> instantiation is limited to access the private members of the instantiation of Stack<int>.
Alternatives include allowing the friendship to extend to all instantiations of the templates;
template <typename T>
class Stack {
template <typename T1>
friend std::ostream& operator<<(std::ostream& os, const Stack<T1>& a);
// ...
};
The implementation for the operator<< could then be done inline inside the class definition, or outside.
You should as well declare function signature inside rob namespace which it actually belongs:
namespace rob {
template <typename T>
class Stack {
friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
};
template < typename T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a){
...
}
In addition to #LibertyPaul's answer you need to add a template<T> to friend std::os... line, in order to work:
namespace rob {
template <typename T> class Stack {
friend std::ostream& operator<< (std::ostream& os, const Stack<T>& a) {
return a.arr.print(os, ' ');
}
};
}

operator<<(ostream&, const BigUnsigned<I>&) must take exactly one argument

I tried to separate the declaration and definition of my templated member function of a templated class, but ended up with the following error and warning.
template <typename I>
class BigUnsigned{
const size_t cell_size=sizeof(I);
std::vector<I> _integers;
public:
BigUnsigned();
BigUnsigned(I);
friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu);
};
std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu){
for (auto integer : bu._integers){
out<<integer<<std::endl;
}
return out;
}
../hw06/bigunsigned.h:13:77: warning: friend declaration
'std::ostream& operator<<(std::ostream&, const BigUnsigned&)'
declares a non-template function [-Wnon-template-friend]
friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu);
^ ../hw06/bigunsigned.h:13:77: note: (if this is not what you
intended, make sure the function template has already been declared
and add <> after the function name here) ../hw06/bigunsigned.h:16:51:
error: invalid use of template-name 'BigUnsigned' without an argument
list std::ostream& operator<<(std::ostream& out, const BigUnsigned&
bu){
^ ../hw06/bigunsigned.h: In function 'std::ostream&
operator<<(std::ostream&, const int&)': ../hw06/bigunsigned.h:17:28:
error: request for member '_integers' in 'bu', which is of non-class
type 'const int'
for (auto integer : bu._integers){
^
When I joined the declaration and definition like this, everything compiles fine.
template <typename I>
class BigUnsigned{
const size_t cell_size=sizeof(I);
std::vector<I> _integers;
public:
BigUnsigned();
BigUnsigned(I);
friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu){
for (auto integer : bu._integers){
out<<integer<<std::endl;
}
return out;
}
};
The purpose was to print member variable _integers to cout. What might be the problem?
P.S.: Using this question I made the function free, but did not help.
BigUnsigned is a template type so
std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu)
Will not work as there is no BigUnsigned. You need to make the friend function a template so you can take different types of BigUnsigned<some_type>s.
template <typename I>
class BigUnsigned{
const size_t cell_size=sizeof(I);
std::vector<I> _integers;
public:
BigUnsigned();
BigUnsigned(I);
template<typename T>
friend std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu);
};
template<typename T>
std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu){
for (auto integer : bu._integers){
out<<integer<<std::endl;
}
return out;
}
The reason the second example works is that since it is declared inside the class it uses the template type that the class uses.
A refinement to the answer by NathanOliver.
With the other answer, all instantiations of the function template are friends of all instatiations of the class template.
operator<< <int> is a friend of BigUnsigned<int> as well as BigUnsigned<double>.
operator<< <double> is a friend of BigUnsigned<double> as well as BigUnsigned<FooBar>.
You can change the declarations a little bit so that
operator<< <int> is a friend of BigUnsigned<int> but not of BigUnsigned<double>.
operator<< <double> is a friend of BigUnsigned<double> but not BigUnsigned<FooBar>.
// Forward declaration of the class template.
template <typename I> class BigUnsigned;
// Forward declaration of the function template
template <typename I>
std::ostream& operator<<(std::ostream& out, const BigUnsigned<I>& bu);
// Change the friend-ship declaration in the class template.
template <typename I>
class BigUnsigned{
const size_t cell_size=sizeof(I);
std::vector<I> _integers;
public:
BigUnsigned();
BigUnsigned(I);
// Grant friend-ship only to a specific instantiation of the
// function template.
friend std::ostream& operator<< <I>(std::ostream& out, const BigUnsigned<I>& bu);
};
To add a third variant that improves the readability a little bit, is to define the friend function inside the class:
#include <iostream>
template <typename T>
class Foo {
int test = 42;
// Note: 'Foo' inside the class body is basically a shortcut for 'Foo<T>'
// Below line is identical to: friend std::ostream& operator<< (std::ostream &os, Foo<T> const &foo)
friend std::ostream& operator<< (std::ostream &os, Foo const &foo) {
return os << foo.test;
}
};
int main () {
Foo<int> foo;
std::cout << foo << '\n';
}

error when trying to overload << operator and using friend function

I am trying to overload << operator and using friend function.
Below code chunk works just fine.
template <class T>
class Mystack{
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
};
Since it is friend function I would obviously want to define it outside the class without using scope resolution operator. But when I try that I get error.
template <class T>
class Mystack{
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
};
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
Below is the code snippet for main
Mystack<int> intstack;
std::cout << intstack;
ERROR : Unresolved extrernal symbol.
P.S: Its not the complete running code. Just a sample. Kindly bear.
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
declares and befriends a non-template operator<< function. So Mystack<int> would have as its friend a non-template function std::ostream& operator<<(std::ostream& s, Mystack<int> const& d);, etc.
template<class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
defines an operator<< function template.
The two are not the same. When you write std::cout << intstack;, the overload resolution rules resolve it to the non-template operator<< function you declared, but it isn't defined, so you get a linker error.
There's no way to define a non-template function for every instantiation of a class template outside the class template. You can, however, befriend a specialization of your operator<< function template:
// forward declarations
template <class T>
class Mystack;
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
template <class T>
class Mystack
{
friend std::ostream& operator<< <T>(std::ostream& s, Mystack<T> const& d);
// ^^^
};
or befriend every specialization of the function template, which is worse from an encapsulation point of view (since, e.g., operator<< <int> would be a friend of Mystack<float>):
template <class T>
class Mystack
{
public:
template <class U>
friend std::ostream& operator<<(std::ostream& s, Mystack<U> const& d);
};
or just define the friend function inside the class.

Using overloaded output operator for templated class: Undefined symbols for architecture x86_64 [duplicate]

I am trying to overload << operator and using friend function.
Below code chunk works just fine.
template <class T>
class Mystack{
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
};
Since it is friend function I would obviously want to define it outside the class without using scope resolution operator. But when I try that I get error.
template <class T>
class Mystack{
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
};
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
Below is the code snippet for main
Mystack<int> intstack;
std::cout << intstack;
ERROR : Unresolved extrernal symbol.
P.S: Its not the complete running code. Just a sample. Kindly bear.
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
declares and befriends a non-template operator<< function. So Mystack<int> would have as its friend a non-template function std::ostream& operator<<(std::ostream& s, Mystack<int> const& d);, etc.
template<class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d)
{
d.print(s);
return s;
}
defines an operator<< function template.
The two are not the same. When you write std::cout << intstack;, the overload resolution rules resolve it to the non-template operator<< function you declared, but it isn't defined, so you get a linker error.
There's no way to define a non-template function for every instantiation of a class template outside the class template. You can, however, befriend a specialization of your operator<< function template:
// forward declarations
template <class T>
class Mystack;
template <class T>
std::ostream& operator<<(std::ostream& s, Mystack<T> const& d);
template <class T>
class Mystack
{
friend std::ostream& operator<< <T>(std::ostream& s, Mystack<T> const& d);
// ^^^
};
or befriend every specialization of the function template, which is worse from an encapsulation point of view (since, e.g., operator<< <int> would be a friend of Mystack<float>):
template <class T>
class Mystack
{
public:
template <class U>
friend std::ostream& operator<<(std::ostream& s, Mystack<U> const& d);
};
or just define the friend function inside the class.

Implementation of friend overload << operator with templates

I can't figure out the correct syntax for templated friend overloaded operators. Using the code (I'm only showing the relevant stuff) from the class header (please don't tell me to change anything from this section):
#include <iostream>
using namespace std;
template <typename T>
class List;
template <typename T>
class Queue;
template <typename T>
class Stack;
template <typename T>
class Node{
private:
friend class List<T>;
friend class Stack<T>;
friend class Queue<T>;
friend ostream& operator<< <>(const ostream&, const List<T>&);
friend ostream& operator<< <>(const ostream&, const Stack<T>&);
friend ostream& operator<< <>(const ostream&, const Queue<T>&);
};
template <typename T>
class List{
public:
friend ostream& operator<< <>(const ostream&, const List<T>&);
};
template <typename T>
class Stack{
public:
friend ostream& operator<< <>(const ostream&, const Stack<T>&);
};
template <typename T>
class Queue{
public:
friend ostream& operator<< <>(const ostream&, const Queue<T>&);
};
This is where the implementation comes into play, and I can't figure out the syntax.
template <typename T>
ostream& operator<<(const ostream& cout, const List<T>& toPrint){ <-- what do I change here?
// irrelevant body
}
template <typename T>
ostream& operator<<(const ostream& cout, const Stack<T>& toPrint){ <-- what do I change here?
// irrelevant body
}
template <typename T>
ostream& operator<<(const ostream& cout, const Queue<T>& toPrint){ <-- what do I change here?
// irrelevant body
}
What do I need to change in each of those three lines to make this work?
I keep getting compiler errors:
error: template-id ‘operator<< <>’ for ‘std::ostream& operator<<(const ostream&, const Queue<Card>&)’ does not match any template declaration
I guess it might be those:
friend ostream& operator<< <>(const ostream&, const List<T>&);
friend ostream& operator<< <>(const ostream&, const Stack<T>&);
friend ostream& operator<< <>(const ostream&, const Queue<T>&);
lines. You didn't specify the type of a template:
friend ostream& operator<< <T>(ostream&, const List<T>&);
friend ostream& operator<< <T>(ostream&, const Stack<T>&);
friend ostream& operator<< <T>(ostream&, const Queue<T>&);
so compiler couldn't match declarations with definitions.
So declarations:
friend ostream& operator<< <T>(const ostream&, const List<T>&);
and definitions:
template <typename T>
ostream& operator<<(ostream& cout, const List<T>& toPrint) {
// irrelevant body
}
Also to prevent compiler from complaining about not declared templates add declarations before class definition.
Final results:
#include <iostream>
using namespace std;
// Class delcarations - we need them to declare functions
template <typename T>
class List;
template <typename T>
class Stack;
template <typename T>
class Queue;
// operator<< function declaration informs compiler that there is function that can be befriended
template <typename T>
ostream& operator<<(ostream&, const List<T>&);
template <typename T>
ostream& operator<<(ostream&, const Stack<T>&);
template <typename T>
ostream& operator<<(ostream&, const Queue<T>&);
// Class definitions
template <typename T>
class Node{
private:
friend class List<T>;
friend class Stack<T>;
friend class Queue<T>;
};
template <typename T>
class List{
public:
friend ostream& operator<< <T>(ostream&, const List<T>&);
};
template <typename T>
class Stack{
public:
friend ostream& operator<< <T>(ostream&, const Stack<T>&);
};
template <typename T>
class Queue{
public:
friend ostream& operator<< <T>(ostream&, const Queue<T>&);
};
// Actual implemetations
template <typename T>
ostream& operator<<(ostream& out, const List<T>& toPrint) {
out << "List";
return cout;
}
template <typename T>
ostream& operator<<(ostream& out, const Stack<T>& toPrint) {
out << "Stack";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const Queue<T>& toPrint) {
out << "Queue";
return out;
}
// Template instantations test
int main() {
List<int> list;
Stack<int> stack;
Queue<int> queue;
cout << list << endl
<< stack << endl
<< queue << endl;
return 0;
}
for me it printed:
List
Stack
Queue
Here and here you have some nice examples.
EDIT: You were right, I made some mistakes. Corrected the answer to the working one.
there are some weird things here.
firstly the error message renders ostream differently, the fact that it qualifies the return value as std::ostream& and not the parameter parsed in seems a bit fishy - but it could be a red herring.
it is always a shame to see "using namespace std;". i consider the using keyword to be absolutely forbidden - so i have no idea what problems or changes this might cause. i simply suspect it is related.
now - to debug this properly i'd suggest replacing the operator and friend with regular function templates and breaking the access levels to allow that test (i.e. remove private etc.), then introduce each element one at a time until you find which part is actually causing the problem.