-1

Essentially, I have a 2D Vector:

std::vector<std::vector<double> > vect = {
    {1, 2, 3},
    {4, 5, 6}, 
    {7, 8, 9}
};

I pass this vector into a function using iterators:

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto row : begin)
    {
        for(auto col : row)
        {

        }
    }   
}

I pass to the function like: Diag(std::begin(vect), std::end(vect))

But I keep getting complained at that there is no matching function, even though I have seen similar range based loops Here.

An example can be found Here

EDIT:

Error Message:

prog.cpp: In instantiation of 'void Diag(Inverse, Inverse) [with Inverse = __gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >]':
prog.cpp:30:39:   required from here
prog.cpp:10:2: error: no matching function for call to 'begin(__gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >*&)'
  for(auto row : &begin)
  ^
prog.cpp:10:2: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:89:5: note:   template argument deduction/substitution failed:
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • 1
    who complained? your wife? – thang Feb 27 '15 at 10:34
  • @thang The compiler, sorry haha! – Phorce Feb 27 '15 at 10:35
  • post the exact error message from the compiler. – thang Feb 27 '15 at 10:35
  • don't you want to go from begin to end? begin is not a collection, so you can't use collection loop like for(auto row : begin) – thang Feb 27 '15 at 10:39
  • @thang I want to go from begin to end, but I just want access to the row/col if that makes sense? Thats why I tried this approach. Any suggestions? – Phorce Feb 27 '15 at 10:41
  • too long to fit in a comment, so i posted it as an answer. – thang Feb 27 '15 at 10:43
  • Ehm..... your code does not match the error message. – Lightness Races in Orbit Feb 27 '15 at 11:49
  • @LightnessRacesinOrbit Please take a look here: http://ideone.com/jzZmX6 Same code, same error. – Phorce Feb 27 '15 at 12:29
  • @Phorce: No, that's not the same code. You made the same mistake that I identified in my answer an hour ago: your question says `for(auto row : begin)`, but your error message (and your code on ideone) says `for(auto row : &begin)`. And this leads to the error messages being different. It's subtle, but one character can make a _big_ difference so please take more care and look for the little details! – Lightness Races in Orbit Feb 27 '15 at 13:53
  • @LightnessRacesinOrbit Ahh I forgot the "&" - Atleast you gave advise regardless of what I missed and I am greatful for this and you wasn't *criticizing too much* thanks – Phorce Feb 27 '15 at 13:58

2 Answers2

1

See the comments following the post for details...

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto row = begin; row != end; row++)
    {
        for(auto col : *row)
        {

        }
    }
}
thang
  • 3,466
  • 1
  • 19
  • 31
0

In this answer I am ignoring the fact that the error message is from a different piece of code than that which you showed us. However, changing for(auto row : &begin) to for(auto row : begin) as you've silently done is sufficient to get back in sync.

The problem is that ranged-for requires a container, but you provided it with a single iterator instead. There are ways to pass (begin,end) into the loop, but the easiest solution is to simply swap the outer ranged-for with a more conventional loop.

Don't forget to iterate by reference too, for performance's sake:

#include <vector>
#include <iostream>

std::vector<std::vector<double> > vect = {
    {1, 2, 3},
    {4, 5, 6}, 
    {7, 8, 9}
};

template<typename Inverse>
void Diag(Inverse begin, Inverse end)
{
    for(auto it = begin; it != end; ++it)
        for(auto& col : *it)
            std::cout << col << ' ';
}

int main()
{
    Diag(std::begin(vect), std::end(vect));
}

(live demo)

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055