Hi, I've been implementing my own array class,
and I've finally started to use it.
I do have one question,

Array<int> ary1(3);ary1(0)=2;ary1(1)=4;ary1(2)=7; //just an array
  Array<int> ary2(3);ary2(0)=1;ary2(1)=2;ary2(2)=6; //just an array
  cout << ary1;
  cout << ary2;
  Array<int> ary3 = ary1+ary2;
  cout << ary3;
//everything till this point works

  //is this possible
  cout << (Array<int>) ary1+ary2;
  return 0;

Do I need to assign a temp Array<int> just for outputting.

This is the interface for my code

//member function of Array<T>
std::ostream& dump(std::ostream &o=(std::cout),char sep=' ');

//free function
std::ostream& operator<<(std::ostream &o,Array<T> &a){
  return a.dump(o,' ');
}

thanks in advance

have you defined the operator+() for your array class? It should return an Array<T>, which will be temporary.

What exactly is the problem?

I can't compile, I'm given this crazy compile error.

matrix.cpp: In function ‘int main()’:
matrix.cpp:464: error: no match for ‘operator<<’ in ‘std::cout <<
Array<T>::operator+(const Array<T>&) [with T = int](((const Array<int>&)((const
Array<int>*)(& a2))))’
/usr/include/c++/4.2/ostream:112: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:121: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT,
_Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:131: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&
(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:169: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:173: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:177: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:92: note:                
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:184: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:106: note:                
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:195: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:204: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:208: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:213: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:217: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:225: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:229: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:120: note:                
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char,
_Traits = std::char_traits<char>]
array.cpp:328: note:                 std::ostream& operator<<(std::ostream&,
Array<U>&) [with T = int]

My declaration for operater + is as follows

Array<T> operator+(const Array<T>&)

I haven't defined a "<<" as a member of Array<T>, but since one of the it works with the temp, I can't see how this could be the problem.

thanks for your reply

This article has been dead for over six months. Start a new discussion instead.