英文:
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 <class T> 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& val = T()) { create(n, val); }
// new operations: size and index
size_type size() const { return limit - data; }
T& operator[](size_type i) { return data[i]; }
const T& 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 > template <class T> void Vec<T>::create() > { > data = avail = limit = 0; > } > template <class T> void Vec<T>::create(size_type n, const T& val) > { > data = alloc.allocate(n); > limit = avail = data + n; > uninitialized_fill(data, limit, val); > } > template <class T> > void Vec<T>::create(const_iterator i, const_iterator j) > { > data = alloc.allocate(j - i); > limit = avail = uninitialized_copy(i, j, data); > } >
> 不带参数的 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:
>
> template <class T> void Vec<T>::create()
> {
> data = avail = limit = 0;
> }
> template <class T> void Vec<T>::create(size_type n, const T& val)
> {
> data = alloc.allocate(n);
> limit = avail = data + n;
> uninitialized_fill(data, limit, val);
> }
> template <class T>
> void Vec<T>::create(const_iterator i, const_iterator j)
> {
> data = alloc.allocate(j - i);
> limit = avail = uninitialized_copy(i, j, data);
> }
>
>
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论