c++ template class unable to fix ostream and istream functions - c++

So I created a Person class that now i'm trying to convert into a template class but I keep getting the following errors:
error: template-id ‘operator<< <std::vector<int, std::allocator<int> > >’ for ‘std::ostream& operator<<(std::ostream&, Person<std::vector<int, std::allocator<int> > >&)’ does not match any template declaration
friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);
error: template-id ‘operator>><std::vector<int, std::allocator<int> > >’ for ‘std::istream& operator>>(std::istream&, Person<std::vector<int, std::allocator<int> > >&)’ does not match any template declaration
friend std::istream& operator>><T>(std::istream&, Person<T>& lass);
I had a look at similar issues people faced and posted here but I couldn't find a solution that would work for me so I decided to ask my own question. Would mean a lot if you guys can help! I'm very new to C++ and its sort of confusing..
here is my code for header and cc
header:
#ifndef _Person_H_
#define _Person_H_
#include <iostream>
#include <sstream>
#include <vector>
template<class T>
class Person{
private:
vector<T> myVector;
public:
Person(const T vec);
T getVector();
ostream& print_on(std::ostream& o);
friend std::istream& operator>><T>(std::istream&, Person<T>& lass);
friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);
};
template<class T> class Person;
template <class T> std::istream& operator>>(std::istream&, Person<T>&);
template <class T> std::ostream& operator<<(std::ostream&, Person<T>&);
#include "Person.cc"
#endif
and here is my cc file
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <class T>
Person<T>::Person(const T vec) : myVector(vec)
{ }
template <class T>
T Person <T>::getVector() {
return myVector;
}
template <class T>
ostream& Person<T>::print(ostream& o) {
return o << "success ";
}
template <class T>
std::ostream& operator<<(std::ostream& o, Person<T>& m) {
return m.print_on(o);
}
template <class T>
std::istream& operator >> (istream& input, Person<T>& lass)
{
vector<T> vectors;
int Vsize,numbers;
cout<<"Enter size of vector"<<endl;
input >> Vsize;
for (int i = 0; i < Vsize; i++)
{
input>> numbers;
vectors.push_back(numbers);
}
lass=Person(vectors);
return input;
}

You need to move the declaration of operator<< and operator>> before the definition of Person, otherwise the compiler won't know that they're templates in the friend declaration. e.g.
template<class T> class Person;
template <class T> std::istream& operator>>(std::istream&, Person<T>&);
template <class T> std::ostream& operator<<(std::ostream&, Person<T>&);
template<class T>
class Person{
private:
vector<T> myVector;
public:
Person(const T vec);
getVector();
ostream& print_on(std::ostream& o);
friend std::istream& operator>><T>(std::istream&, Person<T>& lass);
friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);
};
PS: It'll be better to make operator<< taking const Person<T>& and make print a const member function.

Related

Overloading >> and << in a class template - linkediting errors and I can't understand why

Here's a test class for demonstrating what I mean.
#include <iostream>
template <typename T>
class Test
{
public:
int s;
friend std::istream& operator>>(std::istream& input, Test<T>& test1);
friend std::ostream& operator<<(std::ostream& output, const Test<T>& test1);
};
template <typename T>
std::istream& operator>>(std::istream& input, Test<T>& test1)
{
input >> test1.s;
return input;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const Test<T>& test1)
{
output << test1.s;
return output;
}
int main()
{
Test<int> clasaTest;
std::cin >> clasaTest;
std::cout << clasaTest;
}
If I rewrite it like this
template <typename T>
class Test
{
public:
int s;
template <typename U>
friend std::istream& operator>>(std::istream& input, Test<U>& test1);
template <typename U>
friend std::ostream& operator<<(std::ostream& output, const Test<U>& test1);
};
it will work properly, but I can't really understand why.
Shouldn't it work with the same T declared before? Because the class will have the same generic argument?
friend std::istream& operator>>(std::istream&, Test<T>&);
and
template <typename T> std::istream& operator>>(std::istream&, Test<T>&)
are unrelated functions (the former is non-template contrary to the later).
Simplest fix would be inline definition:
template <typename T>
class Test
{
public:
int s;
friend std::istream& operator>>(std::istream& input, Test& test1)
{
input >> test1.s;
return input;
}
friend std::ostream& operator<<(std::ostream& output, const Test& test1)
{
output << test1.s;
return output;
}
};

C++ friends in template classes, separation of interface and implementation [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
The following code works fine:
Class.h:
#ifndef ClassLoaded
#define ClassLoaded
#include <iostream>
template <class T> class Class{
public:
template <class T> friend std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op);
};
#endif
Class.cpp:
#include "Class.h"
template class Class<int>;
template std::ostream& operator<<(std::ostream& Stream, const Class<int>& Op);
template <class T> std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op){
return(Stream);
}
Main.cpp:
#include "Class.h"
#include <iostream>
using namespace std;
int main(){
Class<int> Test;
cout << Test << endl;
return(0);
}
but the following extended version gives a linker error (unresolved external symbol) and I do more or less understand why. But how to fix it?
Class.h:
#ifndef ClassLoaded
#define ClassLoaded
#include <iostream>
template <class T> class Class{
public:
class SubClass{
public:
friend std::ostream& operator<<(std::ostream& Stream, const SubClass& Op);
};
template <class T> friend std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op);
private:
SubClass Member;
};
#endif
Class.cpp:
#include "Class.h"
template class Class<int>;
template std::ostream& operator<<(std::ostream& Stream, const Class<int>& Op);
template <class T> std::ostream& operator<<(std::ostream& Stream, const typename Class<T>::SubClass& Op){
return(Stream);
}
template <class T> std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op){
Stream << Op.Member;
return(Stream);
}
Main.cpp:
#include "Class.h"
#include <iostream>
using namespace std;
int main(){
Class<int> Test;
cout << Test << endl;
return(0);
}
I guess I need an analogue of the lines
template class Class<int>;
template std::ostream& operator<<(std::ostream& Stream, const Class<int>& Op);
for SubClass and also some sort of template version of
friend std::ostream& operator<<(std::ostream& Stream, const SubClass& Op);
but how to do it?
Edit: As this was claimed to be a duplicated of another question: My question here is very specific (see comments below) and is not answered by the quoted questions or even mentioned there.
Just provide the definitions of the template functions in the .hpp files.
I believe the following should work:
template <class T> class Class {
SubClass member;
public:
class SubClass {
public:
friend std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op) {
return Stream;
}
};
}

output overload using template

So I'm trying to overload the output operator using templates, but I keep running into two errors. They are:
Error C2988 unrecognizable template declaration/definition
and
Error C2143 syntax error: missing ',' before '<'
template <typename E> class SLinkedList; //forward declaration
template <typename E>
ostream& operator<< (ostream& out, const SLinkedList<E>& v); //forward declaration
template <typename E>
class SLinkedList {
public:
template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
template <typename E>
ostream& operator <<(ostream& out, E const SLinkedLst<E>& v) {
while (v->next != NULL) {
out << v->elem;
v->next;
}
return out;
}
<E> is not needed in
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
Just get rid of it and it should compile.
You are also missing a ; at the end of you class. In C++ a class and struct declaration must end with a ;
You have an extra E in
ostream& operator <<(ostream& out, E const SLinkedLst<E>& v) {
^ what is this doing here?
You are also missing a ; at then end of
v->next
You are also using the same template name in
template <typename E>
class SLinkedList {
public:
template <typename E>
friend ostream& operator<< <E>(ostream& out, const SLinkedList<E>& v);
};
Which E is the function referring too? You will need to change it to some other name.
Try that instead
template <typename E>
class SLinkedList {
public:
template <typename T>
friend std::ostream& operator << (std::ostream& out, const SLinkedList<T>& v);
};
template <typename E>
std::ostream& operator << (std::ostream& out, const SLinkedList<E>& v) {
while (v->next != NULL) {
out << v->elem;
v->next;
}
return out;
}

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.

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.