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;
}
};
}
Related
When separating the declaration/definition of (a friend function + a class template) an error occurs:
error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Property<int> const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABV?$Property#H###Z)
Note that when using a friend function without a class template or vice versa everything works fine
but when using the two things together an error occurs.
I tried to instantiate the template in the .cpp file but it didn't succeed.
Also, I have included the .cpp file at end of the .h file but it didn't work either.
class.h
template <class PropertyType>
class Property {
PropertyType m_property;
public:
const PropertyType& operator=(const PropertyType& value);
friend std::ostream& operator<<(std::ostream& os, Property<PropertyType>& other);
};
class.cpp
template <class PropertyType>
const PropertyType& Property<PropertyType>::operator=(const PropertyType& value) {
m_property = value;
return m_property;
}
template <class PropertyType>
std::ostream& operator<<(std::ostream& os, Property<PropertyType>& other) {
os << other.m_property;
return os;
}
template Property<int>;
main.cpp
int main() {
Property<int> num;
num = 100;
std::cout << num << "\n";
}
What is wrong with them when separating the declaration/definition into two files and they are used together?
There are two problems with your code snippet.
The first problem is that you've put the implementation in the source file instead of the header file. So to solve this just move the implementation into the header file.
The second problem is that even if you move the implementation into the source file the program will still not work(Demo). This is because the friend declaration that you currently have(for overloaded opearator<<), is for an ordinary(non-template) function. That is, in your original code operator<< for class template Property<> is not function template, but “ordinary” function instantiated with the class template if needed. It is what we call a templated entity.
But the definition that you've provided in the source file(.cpp) for the overloaded operator<< is for a function template and not for an oridnary function. Thus, for the statement std::cout << num << "\n"; the linker cannot find the definition/implementation corresponding to the ordinary overloaded operator<< for which you had the friend declaration.
There are two ways to solve this:
Method 1
Add a separate parameter clause in the friend declaration.
template <class PropertyType>
class Property {
PropertyType m_property;
public:
const PropertyType& operator=(const PropertyType& value);
template<typename T> //parameter cluase added here
//---------------------------------------------------vvvvv----------------------->const added here
friend std::ostream& operator<<(std::ostream& os,const Property<T>& other);
};
template <class PropertyType>
//----------------------------------------vvvvv---------------------------------->const added here
std::ostream& operator<<(std::ostream& os,const Property<PropertyType>& other) {
os << other.m_property;
return os;
}
Demo
Method 2
Here we provide forward declaration for the overloaded operator<<.
//forward declaration for class template Property
template<typename T> class Property;
//forward declaration for overloaded operator<<
template<typename T> std::ostream& operator<<(std::ostream&,const Property<T>&);//note the const in the second parameter
template <class PropertyType>
class Property {
PropertyType m_property;
public:
const PropertyType& operator=(const PropertyType& value);
//---------------------------------vvvvvvvvvvvvvv---------------------------------> angle brackets used here
friend std::ostream& operator<<<PropertyType>(std::ostream& os,const Property<PropertyType>& other);//also note the const in the second parameter
};
template <class PropertyType>
const PropertyType& Property<PropertyType>::operator=(const PropertyType& value) {
m_property = value;
return m_property;
}
template <class PropertyType>
//----------------------------------------vvvvv---------------------------------->const added here
std::ostream& operator<<(std::ostream& os,const Property<PropertyType>& other) {
os << other.m_property;
return os;
}
Demo
Method 3
If you want to provide the implementation inside the source file instead of the header file, then you should add
template std::ostream& operator<<(std::ostream& os, Property<int>& other);
inside the source file in addition to adding a template parameter clause for the friend declaration as shown below:
class.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <iostream>
template <class PropertyType>
class Property {
PropertyType m_property;
public:
const PropertyType& operator=(const PropertyType& value);
template<typename T> //parameter clause added
//---------------------------------------------------vvvvv--------------------->const added here
friend std::ostream& operator<<(std::ostream& os,const Property<T>& other);
};
#endif
class.cpp
#include "class.h"
template <class PropertyType>
const PropertyType& Property<PropertyType>::operator=(const PropertyType& value) {
m_property = value;
return m_property;
}
template<typename PropertyType>
//----------------------------------------vvvvv------->const added here
std::ostream& operator<<(std::ostream& os,const Property<PropertyType>& other) {
os << other.m_property;
return os;
}
template class Property<int>;
template std::ostream& operator<<(std::ostream& os,const Property<int>& other);
main.cpp
#include <iostream>
#include "class.h"
int main() {
Property<int> num;
num = 100;
std::cout << num << "\n";
}
Demo
The changes that I made include:
Added a separate template parameter clause for the friend declaration. This is so that the friend declaration is for a function template. Moreover, we specify a different type parameter named T and not PropertyType because otherwise the new PropertyTypewill shadow the outerPropertyType`.
Added a low-level const to the second parameter of the overloaded opeartor<<.
In method3, inside source file(class.cpp), added
template std::ostream& operator<<(std::ostream& os,const Property<int>& other);
for the non-member function overloaded operator<<.
I am reading Section 2.4 of the book "C++ tempalte, a complete guide".
I tried to override output operator (<<) as a function template outside the class template Stack<>.
Below is my code, but it doesn't work.
#include <iostream>
#include <string>
#include <vector>
template<class T>
class Stack
{
private:
std::vector<T> v;
public:
void push(T a);
void printOn(std::ostream & os) const;
template <typename U>
friend std::ostream& operator<< (std::ostream& out, const Stack<U> & s);
};
template<typename T>
std::ostream& operator<< (std::ostream out, const Stack<T> & s)
{
s.printOn(out);
return out;
}
template<class T>
void Stack<T>::push(T a)
{
v.push_back(a);
}
template<class T>
void Stack<T>::printOn(std::ostream & out) const
{
for(T const & vi : v)
{out << vi << " ";}
}
int main()
{
Stack<int> s1;
s1.push(12);
s1.push(34);
std::cout << s1;
}
You are just omitting the &, which makes the operator<< inside and outside the class have different function signatures, they are both valid for std::cout << s1, hence the ambiguity
template<typename T>
std::ostream& operator<< (std::ostream& out, const Stack<T> & s)
// ^
{
s.printOn(out);
return out;
}
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined symbol on a template operator overloading function
Here are my source code.
In Number.h
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
using std::istream;
using std::ostream;
template <class T> class Number;
template <class T>
ostream& operator<<(ostream&, const Number<T>&);
template <class T>
istream& operator>>(istream&, Number<T>&);
template <class T>
class Number{
public:
Number(const T &n) :i(n) {}
Number() :i(0) {}
T& operator+(const Number&rhs) const;
T& operator-(const Number&rhs) const;
T& operator*(const Number&rhs) const;
T& operator/(const Number&rhs) const;
friend ostream& operator<< <T> (ostream& , const Number<T>&);
friend istream& operator>> <T> (istream& , Number<T>&);
private:
T i;
};
#endif
And in Number.cpp
#include "Number.h"
template<class T>
T& Number<T>::operator+(const Number&rhs) const
{
return i+rhs.i;
}
template<class T>
T& Number<T>::operator-(const Number&rhs) const
{
return i-rhs.i;
}
template<class T>
T& Number<T>::operator*(const Number&rhs) const
{
return i*rhs.i;
}
template<class T>
T& Number<T>::operator/(const Number&rhs) const
{
return i/rhs.i;
}
template<class T>
ostream& operator<<(ostream&os , const Number<T>&rhs)
{
return os<< rhs.i;
}
template<class T>
istream& operator>>(istream&is , Number<T>&rhs)
{
return is >> rhs.i;
}
I cannot find out why there is
undefined reference to `std::istream& operator>><double>(std::istream&,Number<double>&)'
undefined reference to `Number<double>::operator+(Number<double> const&) const'
errors so on so forth
Use .hpp for template and you can't return reference on a temporary object.
number.h
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
using std::istream;
using std::ostream;
template <class T> class Number;
template <class T>
ostream& operator<<(ostream&, const Number<T>&);
template <class T>
istream& operator>>(istream&, Number<T>&);
template <class T>
class Number{
public:
Number(const T &n) :i(n) {}
Number() :i(0) {}
T operator+(const Number&rhs) const; // Error Here return T not T&
T operator-(const Number&rhs) const;
T operator*(const Number&rhs) const;
T operator/(const Number&rhs) const;
friend ostream& operator<< <T> (ostream& , const Number<T>&);
friend istream& operator>> <T> (istream& , Number<T>&);
private:
T i;
};
#include <number.hpp>
#endif
number.hpp
#ifndef NUMBER_HPP
#define NUMBER_HPP
template<class T>
T
Number<T>::operator+(const Number& rhs) const
{
return i + rhs.i;
}
template<class T>
T
Number<T>::operator-(const Number&rhs) const
{
return i-rhs.i;
}
template<class T>
T
Number<T>::operator*(const Number&rhs) const
{
return i*rhs.i;
}
template<class T>
T
Number<T>::operator/(const Number&rhs) const
{
return i/rhs.i;
}
template<class T>
ostream& operator<<(ostream&os , const Number<T>&rhs)
{
return os<< rhs.i;
}
template<class T>
istream& operator>>(istream&is , Number<T>&rhs)
{
return is >> rhs.i;
}
#endif
main.cpp
#include <iostream>
#include <number.h>
int
main(int, const char**)
{
Number<double> value(1);
Number<double> add(3);
std::cout << value + add << std::endl;
std::cout << value * add << std::endl;
std::cout << value - add << std::endl;
std::cout << value / add << std::endl;
return 0;
}
The definitions of all those member functions need to be available to any translation unit that instantiates the template. Imagine a file that includes Number.h and attempts to use Number<int>. The compiler then needs to generate all of the code for Number instantiated with T as int. How can it do that if it's only seen Number.h? It doesn't know the definition of the member functions.
The fix is to put the definitions of your member functions (everything from Number.cpp) in Number.h. Alternatively, some people like to name Number.cpp as Number.tpp and, instead of #include "Number.h" in Number.cpp, put #include "Number.tpp" at the bottom of Number.h - basically inverting the inclusion so that the header always includes the implementation.
template <>
class test<int> {
int y;
public:
test(int k) : y(k) {}
friend ofstream& operator<< <test<int>> (ofstream& os, const test<int>& t);
};
template<>
ofstream& operator<< <test<int> > (ofstream& os, const test<int>& t)
{
os << t.y;
return os;
}
The code above is specialized template class of test in a int version. I am trying to overload ofstream operator<< function. But it shows error message;
C2027: use of undefined type 'std::basic_ofstream<_Elem,_Traits>'
Besides, the same method works on a ordinary function (not ofstream operator<< but the function that I make) Is there anyway to us operator<< function of ofstream in a specialized template class ?
You need to include
#include <iostream>
At the time of instantiation of the function template. Perhaps you only included
#include <iosfwd>
Besides, you shouldn't be defining (static) friend as a template: https://ideone.com/1HRlZ
#include <iostream>
template <typename> class test;
template <>
class test<int> {
int y;
public:
test(int k) : y(k) {}
friend std::ostream& operator<<(std::ostream& os, const test& t);
};
std::ostream& operator<< (std::ostream& os, const test<int>& t)
{
return os << t.y;
}
int main()
{
test<int> a(42);
std::cout << a << std::endl;
}
Note that it isn't a good idea to have 'using namespace std' in your header file, which is why I removed it from the sample. (It might cause conflicts for users of your header file when they include your header)
There are a number of interesting issues here. First the obvious housekeeping
You should #include <fstream> and don't forget using namespace std.
operator << shouldn't be a template, it should be an overloaded function.
os << t.y confuses the compiler for me (g++ 4.4.3: "warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:"). You intend to push the int to the stream obviously, but the compiler notices that an int can be turned into a test<int> via your constructor and therefore it doesn't know whether you want to push an int or a test<int>. This is stupid I know, and can be solved by making the constructor explicit.
#include <fstream>
using namespace std;
template <typename T>
class test;
template <>
class test<int> {
int y;
public:
explicit test(int k) : y(k) {}
// friend ofstream& operator<< < test<int> > (ofstream& os, const test<int>& t);
friend ofstream& operator<< (ofstream& os, const test<int>& t);
};
// template<>
// ofstream& operator<< <test<int> > (ofstream& os, const test<int>& t)
ofstream& operator<< (ofstream& os, const test<int>& t)
{
os << t.y;
return os;
}
int main() {
}