Sign in to follow this  
glopen

STL: passing containers to template functions (error)

Recommended Posts

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.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this