【问题标题】:using range-for loop: too few arguments to function call, single argument 'a' was not specified使用 range-for 循环:函数调用的参数太少,未指定单个参数“a”
【发布时间】:2014-09-26 06:45:53
【问题描述】:

我是 C++ 初学者,我正在阅读 Bjarne Stroustrup 的C++ 编程语言第 4 版的 3.4.1 Parameterized Types 部分。

本章试图展示的是在用户定义类型Vector 中定义begin()end() 函数,以便range-for loop(即for (auto& s: vs支持))。

定义begin()end() 函数后。我在使用它们时收到错误too few arguments to function call, single argument 'a' was not specified

版本 1

文字确实是这样的:

在 user.cpp 中:

void f2(const Vector<string> & vs)
{
    for (auto& s: vs)          //member function 'begin' not viable: 'this' argument has type 'const Vecto<Std::__1::basic_string<char> >
        cout << s << '\n';
}

Xcode 然后告诉我member function 'begin' not viable: 'this' argument has type 'const Vecto&lt;Std::__1::basic_string&lt;char&gt; &gt;', but function is not marked const.

所以我继续标记函数 const。

在 Vector.h 中:

template<typename T>
class Vector{
    public:
        const T* begin(Vector<T>& a);
        const T* end(Vector<T>& b);
};

在 Vector.cpp 中:

#include "Vector.h"      //get the interface
//To support the range-for loop for our Vector, we must define suitable begin() and end() functions:
template <typename T>
const T* Vector<T>::begin(Vector<T>& x)
{
    return & x[0];
}
template <typename T>
const T* Vector<T>::end(Vector<T>& x)
{
    return x.begin()+x.size(); //pointer to one past last element.
}

错误仍然存​​在。

版本 2

接下来我做的是从函数f2() 中删除const。我收到了这个新错误too few arguments to function call, single argument 'a' was not specified。从 Vector.hVector.cpp 的函数中删除 const 没有任何区别。

在 user.cpp 中:

#include "Vector.h"
void f2(Vector<string> & vs)
{
    for (auto& s: vs)                    //too few arguments to function call, single argument 'a' was not specified
        cout << s << '\n';
}

在 Vector.h 中:

template<typename T>
class Vector{
    public:
        const T* begin(Vector<T>& a);
        const T* end(Vector<T>& b);
};

在 Vector.cpp 中:

#include "Vector.h"       //get the interface
//To support the range-for loop for our Vector, we must define suitable begin() and end() functions:
template <typename T>
const T* Vector<T>::begin(Vector<T>& x)
{
    return & x[0];
}
template <typename T>
const T* Vector<T>::end(Vector<T>& x)
{
    return x.begin()+x.size(); //pointer to one past last element.
}

我已经包含了我认为最关键的部分代码,否则,可以使用完整代码here

【问题讨论】:

    标签: c++ c++11 vector parameterized-types


    【解决方案1】:

    所以我继续标记函数 const。

    你没有标记你的成员函数const。除此之外,它们应该是无参数的。为此,您需要

    const T* begin() const;
                     ^^^^^
    

    请注意,您需要在头文件中提供实现,而不是在单独的.cpp 文件中(除非您将其包含在头的底部,并确保没有尝试编译它。)它提供非常量重载也很有意义。

    template<typename T>
    class Vector{
        public:
            const T* begin() const {return &x[0]; }
            const T* end() const { return &c[0] + size(); }
    
            T* begin() {return &x[0]; }
            T* end()   { return &c[0] + size(); }
    
            size_t size() const { /* return the size */ }
    };
    

    【讨论】:

      【解决方案2】:

      你有两个不同的问题。第一个具有 constness 的原因是您的 beginend 函数未标记为 const,这使得它们无法在常量对象上调用。将声明和定义更改为

      const T* begin(Vector<T>& a) const;
      const T* end(Vector<T>& b) const;
      

      注意函数后面的const 关键字。另请注意,您可以同时拥有同名的常量和非常量函数。

      两个版本中的另一个问题是,您希望函数接受一个参数。他们没有。相反,他们在this 上运行。所以改成这样:

      template <typename T>
      const T* Vector<T>::begin() const
      {
          return &yourInternalVectorVariable[0];
      }
      

      end 函数执行相同操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        • 2017-10-25
        • 1970-01-01
        相关资源
        最近更新 更多