STL: passing containers to template functions (error)

Started by
1 comment, last by glopen 15 years, 8 months ago
I've been having some problems with STL and just when I thought I was getting the hang of things, I get stuck again :( What I want to do is pass a vector to a template function and iterate through it. (I know about std::for_each and use it often, but this is for a different purpose.) This is my code:
template <typename container>
void print(container& c)
{
  typedef typename container::iterator Iter;
  for ( Iter it = c.begin(); it != c.end(); ++it)
  cout<<*it->val;
}
...
std::vector<Obj*> vecObj;
...
print( vecObj );

I get the error: left of '->val' must point to class/struct/union see reference to function template instantiation 'void __cdecl print(const class std::vector<class Obj *,class std::allocator<class Obj *> > &)' being compiled What up? Someone said MSVC6 has problems with template specializations and I do remember seeing a 'Failed to specialize...' for one of the (many) bits of code I tried. Thanks in advance.
Advertisement
"I get the error: left of '->val' must point to class/struct/union"
"*it->val"
If you look at the order of precedence you will see '*' is lower than '->', therefore you are deferencing the iterator and accessing it and then deferencing the structure value . To force the order you want try
(*it)->val

typos fixed.
Typos fixed indeed :D That works! Thanks a lot dmail!
(Precedence.. of all things..)

This topic is closed to new replies.

Advertisement