0

Why the following code doesn't compile?

template<typename T>
std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix)
{
    Matrix<T>::iterator it_begin=matrix.begin();
}

I get the following:

error: expected ';' after expression Matrix::iterator it_begin=matrix.begin();

use of undeclared identifier 'it_begin' Matrix::iterator it_begin=matrix.begin();

In my Generic class Matrix<T> I wrote under public the following:

class iterator;
iterator begin();

and begin implementation is:

template<class T>
typename Matrix<T>::iterator Matrix<T>::begin() {
    return iterator(this, 0);
}

Edit

using the suggested solution I'm getting linker error:

 Undefined symbols for architecture x86_64:  
 "mtm::operator<<(std::__1::basic_ostream<char,
 std::__1::char_traits<char> >&, mtm::Matrix<int> const&)", referenced
 from:
       _main in main.cpp.o ld: symbol(s) not found for architecture x86_64

 clang: error: linker command failed with exit code 1 (use -v to
 see invocation)
dstrants
  • 7,423
  • 2
  • 19
  • 27
  • You need to use `typename Matrix::iterator it_begin=matrix.begin();`, see the linked question for more information. – Sam Varshavchik Jun 14 '20 at 00:28
  • @SamVarshavchik please reopen the question, I am getting linker error (I already tried that and thought it's wrong) –  Jun 14 '20 at 00:37
  • The linker error would be a different reason, and has nothing to do with the asked about compilation error. And the linker error will also close your question, because [it would be a duplicate of this one](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix), or perhaps that template is not in a header file, so your question [would be a duplicate of this one](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). I added both to the list of dupes for this question. – Sam Varshavchik Jun 14 '20 at 00:42

0 Answers0