Question

I am trying to pass a template typedef as argument to a function template. However I get following errors:

TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token

TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token

TestTemplates.cpp:11: error: expected initializer before ‘&’ token

TestTemplates.cpp:25: error: ‘func’ was not declared in this scope

#include <iostream>
#include <vector>

template<class T>
struct MyVector
{
    typedef std::vector<T> Type;
};

template<class T>
void func( const MyVector<T>::Type& myVec )
{
    for( MyVector<T>::Type::const_iterator p = myVec.begin(); p != myVec.end(); p++)
    {
        std::cout<<*p<<"\t";
    }
}

int main()
{
    MyVector<int>::Type myVec;
    myVec.push_back( 10 );
    myVec.push_back( 20 );

    func( myVec );
}

Can anyone point out how to fix this error. I have looked at some posts, but cannot find the solution. Thanks

Était-ce utile?

La solution

You need to tell the compiler that it's a typename

void func( const typename MyVector<T>::Type& myVec )

Then you need to explicitly help the compiler deduce the type for the function:

func<int>( myVec );

BTW, the issue is called "two stage lookup"

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top