Vector模板类的create()函数未定义。

huangapple go评论61阅读模式
英文:

Vector template class create() function not defined

问题

You should define the create function within your Vec class. Here's a translated version of the relevant part:

template <class T> class Vec {
    public:
        // ... (other typedefs and constructors)
        Vec() { create(); } // Default constructor
        // ...

    private:
        iterator data;
        iterator limit;

        // Define the create function
        void create() {
            data = limit = 0;
        }
};

In the code provided, the create function is called by the default constructor (Vec()) to initialize the data and limit pointers to zero, indicating that the vector is empty.

英文:

I have the following excerpt of a template class for a vector, from Koenig's accelerated c++, chapter 11.2:

template &lt;class T&gt; class Vec {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        typedef size_t size_type;
        typedef T value_type;
        Vec() { create(); }
        explicit Vec(size_type n, const T&amp; val = T()) { create(n, val); }
       // new operations: size and index
       size_type size() const { return limit - data; }
       T&amp; operator[](size_type i) { return data[i]; }
       const T&amp; operator[](size_type i) const { return data[i]; }
    private:
       iterator data;
       iterator limit;
};

This code should be the implementation of a vector class.

My doubt is precisely where should the funtion create() be defined? The book explains this function as follows:

> The default constructor, which is the one that takes no arguments, needs to indicate that the Vec is empty (i.e., that it has no elements). It does so by calling a member called create, that we'll have to write. When we return from create, we intend for both data and limit to be set to zero.

But it is never defined?

I've tried to compile the code, but the following error is returned:

main.cpp: In constructor ‘Vec<T>::Vec()’:
main.cpp:9:9: error: there are no arguments to ‘create’ that depend on a template parameter, so a declaration of ‘create’ must be available [-fpermissive]

答案1

得分: 2

> 但它从未被定义?

如果/当你阅读书的后半部分时,你会发现书中引述的 create 成员函数的实现如下:

> 我们将从各种 create 函数开始看起,这些函数负责分配内存,在该内存中初始化元素,并适当地设置指针。在每种情况下,我们初始化分配的内存,因此,在运行 create 后,指针 limit 和 avail 总是相等:最后构造的元素与最后分配的元素相同。当我们执行以下任何函数后,你应该自行验证类不变式是否为真:
> cpp &gt; template &lt;class T&gt; void Vec&lt;T&gt;::create() &gt; { &gt; data = avail = limit = 0; &gt; } &gt; template &lt;class T&gt; void Vec&lt;T&gt;::create(size_type n, const T&amp; val) &gt; { &gt; data = alloc.allocate(n); &gt; limit = avail = data + n; &gt; uninitialized_fill(data, limit, val); &gt; } &gt; template &lt;class T&gt; &gt; void Vec&lt;T&gt;::create(const_iterator i, const_iterator j) &gt; { &gt; data = alloc.allocate(j - i); &gt; limit = avail = uninitialized_copy(i, j, data); &gt; } &gt;

> 不带参数的 create 版本创建一个空的 Vec,所以它的任务是确保指针从零值开始。

英文:

> But it is never defined?

If/when you read later part of the book, you'll find the implementations for create member functions as quoted below from the book:

> We shall begin by looking at the various create functions, which are responsible for allocating
memory, initializing elements in that memory, and setting the pointers appropriately. In each
case, we initialize whatever memory is allocated and so, after running create, the pointers
limit and avail are always equal: The last constructed element is the same as the last
allocated element. You should verify for yourself that the class invariant is true after we have
executed any of the following functions:
>
&gt; template &lt;class T&gt; void Vec&lt;T&gt;::create()
&gt; {
&gt; data = avail = limit = 0;
&gt; }
&gt; template &lt;class T&gt; void Vec&lt;T&gt;::create(size_type n, const T&amp; val)
&gt; {
&gt; data = alloc.allocate(n);
&gt; limit = avail = data + n;
&gt; uninitialized_fill(data, limit, val);
&gt; }
&gt; template &lt;class T&gt;
&gt; void Vec&lt;T&gt;::create(const_iterator i, const_iterator j)
&gt; {
&gt; data = alloc.allocate(j - i);
&gt; limit = avail = uninitialized_copy(i, j, data);
&gt; }
&gt;

>
> The version of create that takes no arguments creates an empty Vec, so its job is to ensure
that the pointers start out with zero values.

huangapple
  • 本文由 发表于 2023年5月11日 17:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定