类型和从属名称 [英] Type and Dependent Name

查看:537
本文介绍了类型和从属名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

手动我可以创建 std :: vector< int> :: iterator 对象like:

Manually I can create a std::vector<int>::iterator object like:

std::vector<int>::iterator i;

所以这里 std :: vector< int& c $ c>是一个类型。但是当我写一个函数:

So here std::vector<int>::iterator is a type. But when I write a function :

template <class T>
std::vector<T>::iterator foo(std::vector<int>::iterator i)
{
    return i;
}

编译器显示警告:

std::vector<T>::iterator' : is a dependent name  not a type

,代码不编译。但是如果在主要我调用这样的函数:

and the code does not compiles. But if in main I call the function like this:

int main()
{
    vector<int> v;
    foo(v.begin());
}

参数 T 应该解决。那么为什么编译器显示错误?

The parameter T should be resolved. Then why compiler is showing error?

推荐答案

你必须在它前面加上Typename
std :: vector< T> :: iterator 取决于模板参数,即 T 。因此,应该使用typename作为前缀:
尝试使用:

You have to prefix it with Typename std::vector<T>::iterator is dependent on a template parameter, namely T.Therefore,you should prefix with it typename: Try using:

typename std::vector<T>::iterator

您可以参考: http://pages.cs.wisc.edu/~driscoll/typename.html

这篇关于类型和从属名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆