相关疑难解决方法(0)

T :: iterator出错,其中模板参数T可能是vector <int>或list <int>

我正在尝试编写一个函数来打印常见STL容器(向量,列表等)的表示.我给函数一个模板参数T,例如,它可能代表向量.我在获取类型为T的迭代器时遇到问题

vector<int> v(10, 0);
repr< vector<int> >(v);
Run Code Online (Sandbox Code Playgroud)

...

template <typename T>
void repr(const T & v)
{
    cout << "[";
    if (!v.empty())
    {
        cout << ' ';
        T::iterator i;
        for (i = v.begin(); 
             i != v.end()-1;
             ++i)
        {
            cout << *i << ", ";
        }
        cout << *(++i) << ' ';
    }
    cout << "]\n";
}
Run Code Online (Sandbox Code Playgroud)

...

brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in …
Run Code Online (Sandbox Code Playgroud)

c++ templates stl repr

6
推荐指数
1
解决办法
7528
查看次数

标签 统计

c++ ×1

repr ×1

stl ×1

templates ×1