开发者

How does a vector<T> get resolved as a function parameter for a templated function?

I have a helper function that creates an Array_Ref obj. The function has a parameter, vector<t> - th开发者_Python百科at the compiler is complaining about. I'm using VS2010.

  • I put the function in a .h by itself.
  • I put the function in Array_Ref.h
  • I put it in a .cpp file.
  • I put typename in front of vector<T>
  • I put typedef typename in front of vector<T>

Nothing seems to work.


#include <vector>
template<class T>
Array_Ref<T> make_ref(vector<T> &v, int s)
{
    return (v.size()) ? Array_Ref<T>(v,s): Array_Ref<T>(0,0);
}

I'm getting:

error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed.
error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '<'

However, putting this in the same header file as Array_Ref.h works just fine:

template<class T,int size>
Array_Ref<T> make_ref(T (&p)[size])
{
    return (p) ? Array_Ref<T>(p,size): Array_Ref<T>(0,0);
}


It's std::vector, not vector. Also, you don't appear to have defined Array_Ref anywhere.


Perhaps the std namespace is missing ? Change vector to std::vector (avoid using namespace directives in header files).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜