Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误C2760:语法错误:意外标记'<';,预期的'';_C++ - Fatal编程技术网

C++ 错误C2760:语法错误:意外标记'<';,预期的'';

C++ 错误C2760:语法错误:意外标记'<';,预期的'';,c++,C++,使用VS2017和代码: template <typename T> void showset(vector<T> v) { for (vector<T>::iterator it = v.begin(); it != v.end(); it++) { cout << *it; } cout << endl; } 模板 无效显示集(向量v) { for(vector::iterator

使用VS2017和代码:

template <typename T>
void showset(vector<T> v)
{
    for (vector<T>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it;
    }
    cout << endl;
} 
模板
无效显示集(向量v)
{
for(vector::iterator it=v.begin();it!=v.end();it++)
{

cout首先注意,如果在这里引用一个类似的
vector::iterator
,那么您需要将
typename
放在前面。此外,取决于
T
是什么,只有当
std::cout
操作符对此的正确解释如下:

template <typename T>
void showset(const T& v)
{
    for (auto const &x : v)
    {
        cout << x;
    }
    cout << endl;
}
template<typename T>
class LogContainerHelper {
    LogContainerHelper(const T& v
                       size_t maxFront,
                       size_t maxTail)
        : mContainer{ v }
        , mMaxFront{ maxFront }
        , mMaxTail{ maxTail }
    {}

    std::ostream &printTo(std::ostream &out) const {
         // here I usually have something more complex
         // depending on mMaxFront and mMaxTail values, 
         // don't have time to recreate that now
         auto it = std::begin(mContainer);
         auto end = std::end(mContainer);

         out << '[';
         if (it != end) {
            out << *it;
            ++it;
         }
         for (; it != end; ++it)
         {
             out << ", " << *it;
         }
         return out << ']';
    }
private:
    const T &mContainer;
    size_t mMaxFront;
    size_t mMaxTail;
};

template<typename T>
std::ostream &operator <<(std::ostream &out, const LogContainerHelper<T> &helper) {
    return helper.printTo(out);
}

template<typename T>
auto LogContainer(const T& v,
                  size_t maxFront = std::numeric_limits<size_t>::max(),
                  size_t maxTail = 0)
        -> LogContainerHelper<T> {
    return LogContainerHelper<T>{ v, maxFront, maxTail };
}
模板
无效展示集(const T&v)
{
用于(自动常数和x:v)
{

请尝试使用
typename vector::iterator it=v.begin();
,或者
auto it=v.begin();
。此外,传递容器等大对象时,不要按值传递,而是按常量引用传递:
void showset(const vector&v)
。这与OP的
void showset(vector v)不同
是的,这是故意的。这更通用,它适用于任何容器。我同意它更通用,只是说这种通用性可能不是OP想要的。如果需要,限制也是一个功能。至少我没有想到他在哪里指定它不是这样。@Bllue如果你想知道w为什么不使用名称空间std;
来放置
,欢迎您阅读。
template <typename T>
void showset(std::vector<T> v)
{
    for (auto& ref : v)
    {
        std::cout << ref;
    }
    std::cout << std::endl;
}
void showset(const std::vector<T>& v);
for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
template <typename T>
void showset(const T& v)
{
    for (auto const &x : v)
    {
        cout << x;
    }
    cout << endl;
}
template <typename T>
void showset(const T& v)
{
    for (auto it = std::begin(v); it != std::end(v); ++it)
    {
        cout << *it;
    }
    cout << endl;
}
template<typename T>
class LogContainerHelper {
    LogContainerHelper(const T& v
                       size_t maxFront,
                       size_t maxTail)
        : mContainer{ v }
        , mMaxFront{ maxFront }
        , mMaxTail{ maxTail }
    {}

    std::ostream &printTo(std::ostream &out) const {
         // here I usually have something more complex
         // depending on mMaxFront and mMaxTail values, 
         // don't have time to recreate that now
         auto it = std::begin(mContainer);
         auto end = std::end(mContainer);

         out << '[';
         if (it != end) {
            out << *it;
            ++it;
         }
         for (; it != end; ++it)
         {
             out << ", " << *it;
         }
         return out << ']';
    }
private:
    const T &mContainer;
    size_t mMaxFront;
    size_t mMaxTail;
};

template<typename T>
std::ostream &operator <<(std::ostream &out, const LogContainerHelper<T> &helper) {
    return helper.printTo(out);
}

template<typename T>
auto LogContainer(const T& v,
                  size_t maxFront = std::numeric_limits<size_t>::max(),
                  size_t maxTail = 0)
        -> LogContainerHelper<T> {
    return LogContainerHelper<T>{ v, maxFront, maxTail };
}
 cout << "Main containter is: " << LogContainer(v) << '\n';