I declared an overloaded operator as a friend in an Xcode C++ program
template <typename T> friend class list_template;
template <typename T> friend ostream& operator<< (ostream &, list_template<T> &);
It gives me an error on the second declaration that list_template has not been declared?
If I #include the file where list_template is declared I get more problems...
You're missing a global forward declaration of list_template, if I understand what you're trying to do:
MyClass.h
// forward declarator. must match definition in list_template.h
template<typename T> class list_template;
class MyClass
{
public:
MyClass() {};
virtual ~MyClass() {};
template<typename T> friend class list_template;
template<typename T> friend ostream& operator <<(ostream&, const list_template<T>&);
};
list_template.h
template<typename T>
class list_template
{
public:
list_template() {};
virtual ~list_template() {};
// declare friend ostream operator <<
friend ostream& operator << <>(ostream& os, const list_template<T>& lt);
};
// ostream insertion operator <<
template<typename T>
ostream& operator <<(ostream& os, const list_template<T>& lt)
{
// TODO: use lt here.
return os;
}
At least I think this is near where you were going.
Related
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.
I have written a C++ STL like bitset class:
template<size_t N>
class bitset {
public:
...........
friend std::ostream& operator << (std::ostream &, bitset<N> const&);
private:
..........
};
// end of class
template<size_t N>
std::ostream& operator << (std::ostream &os, bitset<N> const& rhs) {
............
.........
return os;
}
And I am trying to use it like this:
bitset<5> foo; // success
std::cout << foo << std::endl; // fail
And the error message is -
undefined reference to `operator<<(std::ostream&, bitSet<5u> const&)
What's the problem actually?
Your friend's declaration must be a template as well, just like the definition is:
template <size_t N>
class bitset {
public:
template <size_t M>
friend std::ostream& operator << (std::ostream &, bitset<M> const&);
};
template <size_t M>
std::ostream& operator << (std::ostream &os, bitset<M> const& rhs) {
return os;
}
Alternatively, you could declare the operator<< directly within the class scope:
template<size_t N>
class bitset {
public:
friend std::ostream& operator << (std::ostream & os, bitset const&) {
return os;
}
};
Some possible answers to your question are given here.
In addition to the answer of Piotr S., you can also predeclare the function templates:
template<size_t N> class bitset;
template<size_t N> std::ostream& operator << (std::ostream &, bitset<N> const&);
//now comes your class definition
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.
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.
My question ist related a bit to this one.
I want to overload the operator << for some class and I found two different notations that both work:
template <class T>
class A{
T t;
public:
A(T init) : t(init){}
friend ostream& operator<< <> (ostream &os, const A<T> &a); //need forward declaration
//template <class U> friend ostream& operator<< (ostream &os, const A<U> &a);
};
Do I define identical things with different notations? Or is the first version more restrictive in which instance (in this case only the instance with the same T as my class A) of << is friend of A?
The first version restricts the friendship to the operator<< for the specific type A<T> , while the second makes any operator<< that takes an A<SomeType> a friend.
So yes, the first one is more restrictive:
template<class T>
ostream& operator<< (ostream& os, const A<T>& a) {
A<double> b(0.0);
b.t; // compile error with version 1, fine with version 2
return os;
}
int main() {
A<int> a(0);
cout << a << endl;
}
It so happens that the definition of friend functions have an exception for templates. It allows you to write this:
template <class T>
class A{
T t;
public:
A(T init) : t(init){}
friend ostream& operator<<(ostream &os, const A &a)
{ // Implementation in the class
}
};
And it has the advantage of creating a normal function automatically created for each instance of A<T> you create.
For reference: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16