简体   繁体   English

将参数传递给构造函数

[英]Passing parameter to a constructor

I created a class vector in C++ and then tried to use an vector object in a different class called abc .我在 C++ 中创建了一个 class vector ,然后尝试在另一个名为abc的 class 中使用向量 object 。

What I want to do is define a object in class abc of type vector我想要做的是在 class abc中定义一个vector类型的 object

Something like this:像这样:

#include <iostream>
using namespace std;


class vector{
public:
    double icomponent=1;
    double jcomponent=1;
    double kcomponent=1;

    vector(double conI, double conJ, double conK){   //constructor
         icomponent = conI;
         jcomponent = conJ;
         kcomponent = conK;
    }
};

class abc{
    double i,j,k;
    vector velocity(i,j,k); 
};

However this code doesn't compile and throws this error:但是这段代码不会编译并抛出这个错误:

member "abc::i" is not a type name

I don't understand what is causing the problem.我不明白是什么导致了这个问题。 Can anyone figure it out?任何人都可以弄清楚吗?

I tried searching online, but the closest I could find is- this , but that one was caused by some header file reference issues, and not related to this one.我尝试在线搜索,但我能找到的最接近的是 - 这个,但那个是由一些 header 文件引用问题引起的,与这个无关。

You cannot do what you are trying to do in the way you've defined.你不能按照你定义的方式做你想做的事。 Essentially, you cannot pass the variables i , j , and k to the constructor for velocity in such a manner.本质上,您不能以这种方式将变量ijk传递给velocity的构造函数。 You need a constructor.你需要一个构造函数。 Example:例子:

class abc {
    double i, j, k;
    vector velocity;
public:
    abc( double i, double j, double k ) : i{i}, j{j}, k{k}, velocity{ i, j, k } {}
};

This uses the member initializer list to initialize the three double variables, as well as the velocity variable.这使用成员初始化器列表来初始化三个双变量,以及velocity变量。

Alternatively, as user17732522 mentions in the comments below, if your variables are public, you can do what you were trying with a slight modification:或者,正如 user17732522 在下面的评论中提到的那样,如果您的变量是公开的,您可以通过稍作修改来执行您正在尝试的操作:

class abc {
public:
    double i, j, k;
    vector velocity = vector{ i, j, k };
};

In order to use this method, you must initialize the i , j and k during construction of the object (either through a constructor, or through an aggregate initialization as shown below), otherwise, you will get undefined behaviour by utilizing uninitialized variables in the creation of the velocity object.为了使用这个方法,你必须在 object 的构造过程中初始化ijk (通过构造函数,或者通过如下所示的聚合初始化),否则,你将通过在创建velocity object。

int main()
{
    abc a{ 1, 2, 3 };
    std::cout << a.velocity.icomponent << '\n'; // print 1
    std::cout << a.velocity.jcomponent << '\n'; // print 2
    std::cout << a.velocity.kcomponent << '\n'; // print 3
}

The problem with vector velocity( i, j, k ); vector velocity( i, j, k ); that you were trying, is that the C++ compiler interprets this as a function called velocity which returns a vector , then i , j and k are expected to be types (which of course they are not).您正在尝试的是 C++ 编译器将其解释为称为velocity的 function ,它返回一个vector ,然后ijk应该是类型(当然它们不是)。

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

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