0

Why following code works fine:

template <typename Set, typename Vector> void copySetToVector2(Set &s, Vector &v)
{
    copy(s.begin(), s.end(), inserter(v, v.begin()));
}

int main()
{
    set<int> s1;
    s1.insert(1);
    s1.insert(2);
    s1.insert(3);

    vector<int> v1;

    copySetToVector2(s1, v1);
    return 0;
}

But if I change variables to pointers in template function compiler produces error:

'std::set< int >*' is not a class, struct, or union type

What is wrong here?

Bill
  • 14,257
  • 4
  • 43
  • 55
Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101

1 Answers1

4

If you change this:

template <typename Set, typename Vector> void copySetToVector2(Set &s, Vector &v)

to this:

template <typename Set, typename Vector> void copySetToVector2(Set *s, Vector *v)

then the body needs to look like this:

template <typename Set, typename Vector> void copySetToVector2(Set *s, Vector *v)
{
    copy(s->begin(), s->end(), inserter(*v, v->begin()));
}

The dot notation s.begin() does not work for pointers. You need to switch to s->begin(). See This link for more details.

Community
  • 1
  • 1
Bill
  • 14,257
  • 4
  • 43
  • 55