2

we are porting our application from HP to linux. I have encountered a roadblock trying to compile an important module. Below is a simplified version of that code. This compiles in HP (aCC compiler) but linux g++ 4.8.2 gives below error -

void test
{
    vector<int>::iterator p1;
    vector<int> vec;

    vec.push_back(1);
    vec.push_back(2);

    p1 = vec.begin();

    while ( p1!= vec.end())
    {
      xyz(p1);

      p1++;
    }
}


void xyz(int* i)
{
 //do something here
}

error: no matching function for call to 'DBAccess::xyz(std::vector::iterator&)' xyz(p1); ^ note: candidate is: In file included from DBAccess.pc:18:0: ../include/DBAccess.h:59:7: note: void DBAccess::xyz(int*) void xyz(int* i);

Kishore
  • 21
  • 1
  • 3
    replace ``xyz(p1)`` with ``xyz(&(*p1))`` cause p1 is an iterator so you will have to dereference it – Asesh Feb 23 '18 at 06:16
  • 1
    Pointer is iterator, but not vice versa. Some compilers implement iterator as pointer, but there is no guarantee in C++. Also, please have a better title. – user202729 Feb 23 '18 at 06:27
  • In addition to @Asesh: `xyz(&*p1);` might look like a joke. However, consider that it is not clear how the iterator is implemented. It might be a pointer or something completely different with overloaded `operator*()`. Considering this, it starts to make sense, – Scheff's Cat Feb 23 '18 at 06:27
  • Alternatively you can get raw pointer with `std::vector::data()`. – user202729 Feb 23 '18 at 07:20
  • thank you. I understand now. I was stuck on thinking why it compiled fine with aCC not g++ instead of thinking as a programmer. – Kishore Feb 23 '18 at 07:27
  • See also this earlier question: https://stackoverflow.com/questions/41853089/does-using-a-pointer-as-a-container-iterator-violate-the-standard – MSalters Feb 23 '18 at 09:33

0 Answers0