简体   繁体   English

如何编写构造函数 function 来设置向量的大小,该向量是 C++ 中的 class 的属性?

[英]How do I write the constructor function that sets the size of a vector which is the attribute of my class in C++?

The attribute of my class is a vector whose size represents the number of dimensions of a vector.我的 class 的属性是一个向量,它的大小代表一个向量的维数。 I have to write a constructor that makes a null vector for a given size.我必须编写一个构造函数,为给定大小生成 null 向量。 I'm not sure how to do it, this is what I've tried:我不知道该怎么做,这是我尝试过的:

Vecteur(size_t d)
    : coord.size(d) { for( auto c : coord){ c = 0;}}

This is the error I get:这是我得到的错误:

图片

Your can't initialize a member of your class member in using initializer list but you can only use constructors.您无法在使用初始化列表时初始化 class 成员的成员,但您只能使用构造函数。

So your declare a constructor for your vector which initializes its member to zero then use this constructor in your class initializer list所以你为你的向量声明一个构造函数,将它的成员初始化为零,然后在你的 class 初始化列表中使用这个构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM