具有自定义模板的 STL 迭代器

发布于 2024-07-14 19:40:11 字数 524 浏览 3 评论 0原文

我有以下模板方法,

template <class T>
void Class::setData( vector<T> data )
{    
    vector<T>::iterator it;
}

并且收到以下编译错误( XCode/gcc )

错误:预期为“;” 在“它”之前

我发现其他人也有类似的问题 这里 (往下读会发现它是相同的,即使它开始时有不同的问题),但他们似乎已经通过更新 Visual Studio 解决了。 这让我猜测这是一个编译器问题并且它应该编译,对吗? 通过从 0 到 size 的索引进行迭代是可行的,但这不是我希望实现此函数的方式。 还有其他办法解决这个问题吗? 谢谢

i have the following template method,

template <class T>
void Class::setData( vector<T> data )
{    
    vector<T>::iterator it;
}

and i'm getting the following compilation error ( XCode/gcc )

error: expected `;' before 'it'

i found someone else with a similar problem here (read down to see it's the same even though it starts out with a different issue) but they seem to have resolved by updating Visual Studio. This makes me guess that it is a compiler issue and that it should compile, is that correct? Iteration via indexing from 0 to size works, however it is not the way i would prefer to implement this function. Is there another way around this?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-07-21 19:40:12

何时使用 typename 关键字的经典案例。 希望您拥有#include-ed 向量迭代器,并在范围内的某个位置拥有using namespace std;。 用途:

typename vector<T>::iterator it;

查找从属名称。 从此处开始。

Classic case of when to use the typename keyword. Hoping that you have #include-ed vector and iterator and have a using namespace std; somewhere in scope. Use:

typename vector<T>::iterator it;

Look up dependent names. Start here.

自控 2024-07-21 19:40:12

我认为您缺少 typename

#include <vector>
using namespace std;

class Class{
public:
    template <class T>
    void setData( vector<T> data ) {
        typename vector<T>::iterator it;
    }
};

I think you are missing a typename:

#include <vector>
using namespace std;

class Class{
public:
    template <class T>
    void setData( vector<T> data ) {
        typename vector<T>::iterator it;
    }
};
北笙凉宸 2024-07-21 19:40:12

尝试:

template <class T>
void Class::setData( std::vector<T> data )
{    
    std::vector<T>::iterator it;
}

是否缺少 using 语句?

Try:

template <class T>
void Class::setData( std::vector<T> data )
{    
    std::vector<T>::iterator it;
}

Just is case it's a missing using statement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文