text: ++ has no idea how to evaluate x > y!!
  c:\consoleapplication1\main.cpp(55): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'Cents' (or there is no acceptable conversion)
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(497): note: could be 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_streambuf> *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(477): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(const void *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(457): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(long double)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(437): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(double)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(417): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(float)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(396): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned __int64)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(376): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(__int64)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(355): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned long)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(335): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(long)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(315): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned int)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(290): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(int)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(270): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(unsigned short)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(236): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(short)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(216): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(bool)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(209): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(202): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_ios> &(__cdecl *)(std::basic_ios> &))'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(196): note: or 'std::basic_ostream> &std::basic_ostream>::operator <<(std::basic_ostream> &(__cdecl *)(std::basic_ostream> &))'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(694): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const char *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(741): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,char)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(779): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const char *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(826): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,char)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(952): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const signed char *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(959): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,signed char)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(966): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const unsigned char *)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(973): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,unsigned char)'
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(983): note: or 'std::basic_ostream> &std::operator <<,T>(std::basic_ostream> &&,const _Ty &)'
  1> with
  1> [
  1> T=Cents,
  1> _Ty=Cents
  1> ]
  1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(1021): note: or 'std::basic_ostream> &std::operator <<>(std::basic_ostream> &,const std::error_code &)'
  1> c:\consoleapplication1\main.cpp(55): note: while trying to match the argument list '(std::ostream, Cents)'
  Remember what I said about crazy error messages? We hit the motherload!!
  With the cent class.
  ->I'm getting an error:
  template <class T> Cents average(Cents *array, Cents length)
  no instance of function template "average" matches the argument list
  argument types are: (Cents[4],int)
  I don't get it what i'm doing wrong ?
  Thank you.
  Two things that I can see:
  1) average() doesn't need to be a template function since you're not using type T for anything.
  2) You defined your length parameter as type Cents, when it should be type int. This is causing a mismatch.
  Yes thank you, i did modify the length into type int and it works. Works with templates too if i modify the function a little.
  Thank you.
  Hi Alex,
  Regarding the overloaded << operator. At first I was getting errors and I wasn't sure what went wrong but upon debugging I realized that it should be std::ostream instead of just ostream itself. I don't know if it varies by compiler or it was a mistake. I thnk this is worth noting.
  Hi Hexx!
  It should always be @std::ostream. You can only use @ostream if you're "using namespace std;", which you shouldn't do.
  Yup, this was an unintentional omission. Fixed!
  Quick question, in this, you use both
  and
  Is there a difference between these? Googling shows some people saying no difference, but others are saying that they are almost identical bar a few occasions where you have to use one instead of the other (I don't understand when though). Anyone able to provide clarity on this?
  They're basically the same. There was a language design bug where only template could be used when doing templated templates, so most people adopted template class over template typename since it worked more broadly. However, this issue has been fixed as of C++17, so as far as I'm aware, they're identical now.
  Hi Jack!
  Quoting the latest standard draft: "There is no semantic difference between class and typename in a type-parameter-key."
  N4762, § 12.1, 2
  How does array[count] work on passing in a Cents object even though we have not overloaded the operator [] for an object of type Cents? I didn't understand that part.
  Hi Omkar!
  This isn't the operator[] of @Cents but the operator[] of an array, the data type doesn't matter. It returns the @Cents object at index @count.
  References
  Chapter 6
  Hi Alex,
  maybe this question is more suitable for the chapter about Operator overloading, but I saw it here, so I ask here :). It's about overloading operator += (and /=). Your += operator overload
  has return type void, which makes it unusable in (e.g.):
  I admit, this kind of code is neither nice nor often to see, but it's possible. If we instead implement it as
  and return *this pointer, it will allow us to use operation as shown above. I have already seen both implementations elsewhere. Could you please comment on that?
  You are correct -- it should return a Cents& so the result of operator+= can be chained with other operators. It wasn't necessary for this particular example, so I didn't think about it at the time. I've updated the example. Thanks!
  Alex,
  You have started from "template <typename T> " and suddenly, there is "template <class T> in "Another Example". Stack overflow states they are almost identical. Any comments? Thank you.
  Hello,
  Thank you for such a great tutorial!
  I just have a quick question. Why could you use just use
  instead of
  (or something of the sort) but you had to use
  for the friend function? Do you have to use it like that for friend functions or?
  Thank you,
  Scratch Cat
  For member functions, m_cents refers to the implicit object (essentially, this->m_cents). Friend functions aren't member functions though, so there is no implicit object. Therefore, we need to explicitly supply the object to access (e.g. c1)
  The compiler says, “oh, we want to call max(int, int)”
  =>
  The compiler says, “oh, we want to call int max(int, int)”
  I think this one is okay as is. It's common to omit the return value type when referencing which function is called, since functions with the same name and parameters can't have different return values anyway.
  "because future lessons will build off of some of these concepts"
  ->
  "because future lessons will build off *on* some of these concepts"
  It is correct as written. This is a common English phrase.
  Hi Alex!
  just wanna point out a typo in
  friend ostream& operator<< (ostream &out, const Cents ¢s)
  {
  out << cCents.m_cents << " cents ";
  return out;
  }
  there is a typo it should be "cents.m_cents". This typo is only when u talk about we need to operate std::cout
  Thanks, fixed.
  this was causing the issue:
  template <class T>
  Cents average(Cents *array, int length) //this should be Cents average(T *array, int length) something to do with template specialization
  {
  Cents sum = 0;
  for (int count=0; count < length; ++count)
  sum += array[count];
  sum /= length;
  return sum;
  }
  This is not compiling I guess I have followed all the steps correctly, Can you tell me why?
  #include<iostream>
  using namespace std;
  class Cents
  {
  private:
  int m_cents;
  public:
  Cents(int cents)
  : m_cents(cents)
  {
  }
  friend ostream& operator<< (ostream &out, const Cents ¢s)
  {
  out << cents.m_cents << " cents ";
  return out;
  }
  void operator+=(const Cents cents)
  {
  m_cents += cents.m_cents;
  }
  void operator/=(const int value)
  {
  m_cents /= value;
  }
  };
  template <class T>
  Cents average(Cents *array, int length)
  {
  Cents sum = 0;
  for (int count=0; count < length; ++count)
  sum += array[count];
  sum /= length;
  return sum;
  }
  int main()
  {
  Cents array3[] = { Cents(5), Cents(10), Cents(15), Cents(14) };
  cout << average(array3, 4) << '\n';
  return 0;
  }
  Solutions to make your program compile:
  1) there seems to be a problem with cout << average(array3, 4) << ‘n’
  try cout << average(array3, 4) << ‘n’; // the ‘n’ vs ‘n’
  when you copy ‘n’ from comment section something weird happens
  2) Option 1: comment the template <class T> line
  Option 2:
  template <class T>
  T average(T *array, int length)
  {
  T sum = 0;
  for (int count=0; count < length; ++count)
  sum += array[count];
  N.B. as to why… maybe somebody else can help
  Final code:
  #include<iostream>
  using namespace std;
  class Cents
  {
  private:
  int m_cents;
  public:
  Cents(int cents)
  : m_cents(cents)
  {
  }
  void operator/=(const int value)
  {
  m_cents /= value;
  }
  };
  template <class T>
  T average(T *array, int length)
  {
  T sum = 0;
  for (int count=0; count < length; ++count)
  sum += array[count];
  sum /= length;
  return sum;
  }
  int main()
  {
  Cents array3[] = { Cents(5), Cents(10), Cents(15), Cents(14) };
  cout << average(array3, 4) << 'n';
  return 0;
  }
  could we have overloaded [] instead of += ?
  Yes, but it's better to keep the meaning of the operators as close to the original as possible. Users would not expect [] to do the equivalent of +=.
  Hello! For our function calculating the average of an array, wouldn't it be better if the return type was float? I mean, ok we may have an array of integers, but the average of them might be a floating point number.
  Nice tutorials btw, I love your style of coding AND teaching!
  You could reasonably argue that you should always return a float since the average of ints could return a fractional number. But I think it's useful to see an example of a templated return value. And much like we can say doing integer division produces an integer result, we could say that doing an integer average will also produce an integer result.
  There's no right or wrong answer here, it's more about making it clear what the behavior is.
  Is there any way to print class object name on the screen? While comparing two objects parameters I got the bigger one and I want to print that object name, is there any way?
  You could use "typeid(obj).name()", where obj is your class object. The output of this may vary by compiler.
  Thanks for the great tutorials, Alex!
  I can use some help compiling the first example.
  If I compile this (g++ std=c++11 on ubuntu 14.04) I get the error:
  main.cpp: In instantiation of ‘const T& max(const T&, const T&) [with T = Cents]’:
  main.cpp:30:36: required from here
  main.cpp:6:15: error: no match for ‘operator>’ (operand types are ‘const Cents’ and ‘const Cents’)
  return (x > y) ? x : y;
  ^
  main.cpp:6:15: note: candidate is:
  main.cpp:19:17: note: bool operator>(Cents&, Cents&)
  friend bool operator>(Cents &c1, Cents &c2)
  ^
  main.cpp:19:17: note: no known conversion for argument 1 from ‘const Cents’ to ‘Cents&’
  If I change line 17, not passing by reference from
  to
  it compiles!
  Another way to get it compiling is not passing any variable by reference (in the template).
  Yet another interesting way that does pass the compiler is changing line 4 from:
  I do not understand why the first example does not compile.
  Can anyone help? (I hope it's not an obvious typo...I have looked for it)
  The parameter for operator< weren't properly const. I've updated the examples in the lesson. Thanks for pointing this out!
  Hi Alex, in the section 'Operators, function calls, and function templates' after the box that displays the error there is a typo.
  'operator &;gt;' instead of 'operator >'
  Thank you for the site, it really helps a lot.
  Thanks for pointing that out. It's fixed now.
  #include "stdafx.h"
  #include<stdio.h>
  #include<iostream>
  using namespace std;
  template<class T1>
  class player
  {
  private:
  T1 score;
  public:
  player(T1 n){score =n;}
  friend ostream& operator<<(ostream& cout, player <T1> p);
  };
  template <class T1>
  ostream& operator<<(ostream& cout, player <T1> p)
  {cout<<p.score;return cout;}
  void main()
  {
  player<int> p1(3);
  cout<<p1;
  system("pause");
  }
  when a << operator is overloaded for template class object, the body of the friend function must follow the declaration immediately. If I write the body outside the class like above, it gives error LNK1120 and LNK2019, linking errors.
  whereas, if the class is not template, it doesn't matter whether the body of operator overloading friend function is inside or outside the class, like you said "either way works"
  is there any way by which we can overload << operator in template class by writing the body of the function outside the class(its working when I write it inside the template class)
  This topic gets complex fast.
  Short answer:
  Explanation: read this article
  why is the body of a friend function follows the declaration immediately here, and its outside the class everywhere else
  shouldn't it be more like this? -----
  friend bool operator>(cents &c1, cents &c2)
  };
  //class ends here
  bool operator<(cents &c1, cents &c2) //actual body of method outside the class
  {
  return(c1.m_nCents > c2.m_nCents)
  }
  same question while overlaoding operator<<
  Either way works.
  hi Alex,
  Want to know that when Cents cBigger = max(cNickle, cDime) line is executed will copy constructor invoke to initialize cBigger ? if not then how cBigger will initialize?
  (Edit: When this question was asked and answered, max() was returning by value. I've since changed max to return a const reference)
  Yes, since function max() is returning a class by value, it will use the copy constructor to do so. However, note that because we're using this value as an initializer in this case, the copy constructor may be elided.
  Hi everyone, if you found this code no run normally in your computer as you expect,
  because ostream is in the namespace std, don't forget it.
  Hey Alex,
  Thanks~
  Yes, in this case the compiler will try to convert integer 0 to a Cents object, which it will be able to do via the Cents(int) constructor, and then this will be copied into cCents. Although this may seem less performant that doing a direct initialization (e.g. Cents cCents(0)), your compiler will probably elide the copy (prevent it) and just initialize cCents directly.
  #include<iostream>
  using namespace std;
  template<typename Type>
  Type Max(Type tX,Type tY)
  {return (tX>tY)?tX:tY;}
  class Cents
  {
  private:
  int m_nCents;
  public:
  Cents(int nCents)
  : m_nCents(nCents)
  {
  }
  friend bool operator>(Cents &c1, Cents&c2)
  {
  return (c1.m_nCents > c2.m_nCents) ? true: false;
  }
  };
  int main()
  {
  Cents cNickle(5);
  Cents cDime(10);
  Cents cBigger=Max(cNickle, cDime);
  std::cout<<cBigger;
  I removed it but it is giving me error message Invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'Cents')
  That is a different issue, as you're trying to send cBigger to operator<< without having overloaded the operator.
  It wouldn't be smarter/quicker (for this concrete case) to simply add a typecast from Cents to Ints?
  operator int() { return m_nCents; }//(Because is m_nCents type)
  …or there is some downside am I overlooking?
  For what purpose are you thinking this would be beneficial?
  Well, I guess my point here was not recreate all operator definitions, just do only the typecast and let C++ deal with operators using typecasting.
  But I don't know if doing this implies some downside!.
  Suppose you'll need to overload 20 operators. Would then just make a single typecast overload much quicker?
  Wouldn't be then just make a single typecast overload much quicker? (I forgot the verb!)
  The verb you're looking for is "type conversion operator".
  This is a suitable way to proceed for cases where you don't want to define an overloaded operator when a standard operator will do.
  hey alex,
  template<typename type>
  and
  template<class type>
  whats the difference?
  They're largely identical.
  Scott Myers recommends using "class" when it is expected that the type will always be a class, and "typename" if other types (such as int or char*) may be expected.
  One reason to prefer "class" over "typename" is because class will work with template template arguments, whereas typename will not. I presume this will be fixed in the next version of C++, but as of C++14 it's still not.
  Personally, I just always use class.
  Hi,
  what the meaning of
  Cents tSum = 0;
  implicit convert from int to Cents?
  Yes. The compiler will try to find a way to implicitly convert 0 into a Cents object via a matching constructor..
  I've updated the code to include more full examples.
  is treated as a copy-initialization (not an assignment).
  what is difference between template <typename T> and template <class T>.
  Form me both are genrating same result.
  Prashant. Better to use 'typename' for "function template" and 'class' for "class template"
  The only difference is that you must use "class" instead of "typename" when doing templates of templates (and I think this restriction will be relaxed in a future version of C++).
  Otherwise they are identical.
  could you tell me why there is a friend for the << operator?
  Thanks
  Read to understand the use of "friend function" to overload an operator..
  Name (required)
  Website