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 );
STL: passing containers to template functions (error)
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:
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.
"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.
"*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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement