c++ - 使用 range-for 循环 : too few arguments to function call, 未指定单个参数 'a'

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

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

本章试图展示的是定义 begin()end()用户定义类型中的函数 Vector这样一个 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<Std::__1::basic_string<char> >', 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

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

在 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 .

最佳答案

So I went ahead and marked the functions 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 */ }
};

关于c++ - 使用 range-for 循环 : too few arguments to function call, 未指定单个参数 'a',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25107754/

相关文章:

c++ - 静态链接英特尔 tbb 的问题

c++ - 将有效的QFile转换为QString-QString为空

c++ - std::move 实现

c++ - SDL 的自定义运算符

c++ - 从析构函数调用线程 vector

C# 将生成的数字列表转换为 int 数组

c++ - 如何在 OpenCV 上创建一个 Mat 来制作直方图

c++ - 如何从二进制数据调用函数

javascript - 在 OSX 上运行 C++ SpiderMonkey 会产生链接器错误

c++ - vector 中的斐波那契数列