Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在初始化列表中使用“this”时出错_C++ - Fatal编程技术网

C++ 在初始化列表中使用“this”时出错

C++ 在初始化列表中使用“this”时出错,c++,C++,我得到以下代码的错误“预期标识符”。如何在构造函数中正确使用初始化列表 tanVec::tanVec(const int x, const int y, const int z): this->x(x), this->y(y), this->z(z) { } 您可以安全地删除此-消除歧义不需要它,因为初始化列表中的名称将解析为类的成员,即使参数列表中的参数名称需要在构造函数体中消除歧义 // Compiler will not confuse members x, y, a

我得到以下代码的错误“预期标识符”。如何在构造函数中正确使用初始化列表

tanVec::tanVec(const int x, const int y, const int z): this->x(x), this->y(y), this->z(z)
{

}

您可以安全地删除
-消除歧义不需要它,因为初始化列表中的名称将解析为类的成员,即使参数列表中的参数名称需要在构造函数体中消除歧义

// Compiler will not confuse members x, y, and z with constructor arguments x, y, and z
tanVec::tanVec(const int x, const int y, const int z): x(x), y(y), z(z) {}

您可以安全地删除
-消除歧义不需要此,因为初始化列表中的名称将解析为类的成员,即使参数列表中的参数的名称需要在构造函数体中消除歧义

// Compiler will not confuse members x, y, and z with constructor arguments x, y, and z
tanVec::tanVec(const int x, const int y, const int z): x(x), y(y), z(z) {}

您可以安全地删除
-消除歧义不需要此,因为初始化列表中的名称将解析为类的成员,即使参数列表中的参数的名称需要在构造函数体中消除歧义

// Compiler will not confuse members x, y, and z with constructor arguments x, y, and z
tanVec::tanVec(const int x, const int y, const int z): x(x), y(y), z(z) {}

您可以安全地删除
-消除歧义不需要此,因为初始化列表中的名称将解析为类的成员,即使参数列表中的参数的名称需要在构造函数体中消除歧义

// Compiler will not confuse members x, y, and z with constructor arguments x, y, and z
tanVec::tanVec(const int x, const int y, const int z): x(x), y(y), z(z) {}


只需删除
此->
您不需要它(使参数名称与您的成员变量不同,例如添加ux)。我认为您甚至不需要使用初始化列表来改变名称。您不必使构造函数参数名称与成员名称不同
C::C(int x):x(x){}
完全有效。@user2357112是的,我应该指定该部分为可选部分。我只是觉得这更清楚了。也许就是我。@Celeritas:在正文中,您需要
this->
。只需删除
this->
,您就不需要它了(使参数名称不同于您的成员变量,例如添加389;)。我认为您甚至不需要使用初始化列表来改变名称。您不必使构造函数参数名称与成员名称不同
C::C(int x):x(x){}
完全有效。@user2357112是的,我应该指定该部分为可选部分。我只是觉得这更清楚了。也许就是我。@Celeritas:在正文中,您需要
this->
。只需删除
this->
,您就不需要它了(使参数名称不同于您的成员变量,例如添加389;)。我认为您甚至不需要使用初始化列表来改变名称。您不必使构造函数参数名称与成员名称不同
C::C(int x):x(x){}
完全有效。@user2357112是的,我应该指定该部分为可选部分。我只是觉得这更清楚了。也许就是我。@Celeritas:在正文中,您需要
this->
。只需删除
this->
,您就不需要它了(使参数名称不同于您的成员变量,例如添加389;)。我认为您甚至不需要使用初始化列表来改变名称。您不必使构造函数参数名称与成员名称不同
C::C(int x):x(x){}
完全有效。@user2357112是的,我应该指定该部分为可选部分。我只是觉得这更清楚了。也许只有我。@Celeritas:在身体里,你需要
这个->