鱼C论坛

 找回密码
 立即注册
查看: 1636|回复: 3

[已解决]c++ const 传参

[复制链接]
发表于 2023-7-30 10:37:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我定义了一个向量类,声明是这样:
  1. class Vector
  2. {
  3.         public:
  4.                 unsigned short dimensions = 0;
  5.                 double *values = nullptr;

  6.                 Vector(const Vector &);
  7.                 Vector(unsigned short, double []);
  8.                 ~Vector();
  9.                 Vector operator+(const Vector &);
  10.                 Vector operator+(const double);
  11.                 Vector operator-();
  12.                 Vector operator-(const Vector &);
  13.                 Vector operator-(const double);
  14.                 Vector operator*(const Vector &);
  15.                 Vector operator*(const double);
  16.                 Vector operator/(const double);
  17.                 double dot(const Vector &);
  18.                 double length();
  19.                 Vector length(const int);
  20.                 double angle(const Vector &);
  21. };
复制代码

在 Vector::angle 函数的实现中编译器发出了错误:
  1. #include <cmath>
  2. double Vector::angle(const Vector &other)
  3. {
  4.         return acos(this->dot(other) / (this->length() * other.length()));
  5. };
复制代码

错误是这么说的:
line 4: return acos(this->dot(other) / (this->length() * other.length()));
没有与参数列表和对象匹配的 重载函数 "Vector::length" 实例(对象包含阻止匹配的类型限定符)
C/C++(1087)
这是怎么回事,是 const 的问题吗?应该怎样修复?
最佳答案
2023-7-30 10:37:41
是的,错误是由于  const  修饰符引起的。在 C++ 中,如果成员函数被声明为  const ,表示它不会修改对象的状态,即它不会修改成员变量的值。在你的情况下,编译器指出了问题: Vector::length  函数没有一个  const  修饰符的重载版本,所以无法在  Vector::angle  函数中调用  this->length()  和  other.length() 。

要修复这个问题,你需要在  Vector::length  函数声明中添加  const  修饰符,表示它是一个只读函数,不会修改对象的状态。修改方式如下:

  1. class Vector
  2. {
  3. public:
  4.     // ... 其他成员函数

  5.     double length() const; // 在这里添加 const 修饰符
  6. };
复制代码


然后,在实现文件中对应的函数定义也要加上  const  修饰符:

  1. #include <cmath>

  2. double Vector::length() const // 在这里添加 const 修饰符
  3. {
  4.     // 函数实现
  5. }
复制代码


通过在  Vector::length  函数声明和定义中添加  const  修饰符,编译器就能够正确地识别  length()  函数为只读函数,并允许在  Vector::angle  函数中调用  this->length()  和  other.length()  了。

这样,问题就会得到解决,编译器不再报告关于缺少  const  修饰符的错误,而你的代码也能正确运行。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-30 10:37:40 | 显示全部楼层
错误是因为在成员函数Vector::length()的名称与参数列表中的另一个成员函数Vector::length(const int)冲突,编译器无法确定要调用哪个函数。这是一个名称隐藏的问题。解决方法是在调用时显式指定要调用的函数。

Vector::angle()函数的实现中,将以下代码:

  1. return acos(this->dot(other) / (this->length() * other.length()));
复制代码


修改为:

  1. return acos(this->dot(other) / (this->length()() * other.length()));
复制代码


通过在this->length()后面添加额外的括号,明确指定要调用的是不带参数的Vector::length()函数。这样就可以解决名称冲突的问题。

请注意,在修复了上述问题后,确保所调用的Vector::length()函数返回的是非零值,以避免除以零的错误。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-30 10:37:41 | 显示全部楼层    本楼为最佳答案   
是的,错误是由于  const  修饰符引起的。在 C++ 中,如果成员函数被声明为  const ,表示它不会修改对象的状态,即它不会修改成员变量的值。在你的情况下,编译器指出了问题: Vector::length  函数没有一个  const  修饰符的重载版本,所以无法在  Vector::angle  函数中调用  this->length()  和  other.length() 。

要修复这个问题,你需要在  Vector::length  函数声明中添加  const  修饰符,表示它是一个只读函数,不会修改对象的状态。修改方式如下:

  1. class Vector
  2. {
  3. public:
  4.     // ... 其他成员函数

  5.     double length() const; // 在这里添加 const 修饰符
  6. };
复制代码


然后,在实现文件中对应的函数定义也要加上  const  修饰符:

  1. #include <cmath>

  2. double Vector::length() const // 在这里添加 const 修饰符
  3. {
  4.     // 函数实现
  5. }
复制代码


通过在  Vector::length  函数声明和定义中添加  const  修饰符,编译器就能够正确地识别  length()  函数为只读函数,并允许在  Vector::angle  函数中调用  this->length()  和  other.length()  了。

这样,问题就会得到解决,编译器不再报告关于缺少  const  修饰符的错误,而你的代码也能正确运行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-30 10:37:58 | 显示全部楼层
关于错误信息:

编译器报错指出在 Vector::angle 函数的实现中,没有与参数列表和对象匹配的重载函数 "Vector::length" 的实例。这意味着编译器无法找到匹配的函数重载形式来调用 length() 函数。

修复方法:

问题出在你在类定义中同时声明了两个名为 length 的成员函数,其参数列表不同:
  1. double length();           // 第一个 length() 函数
  2. Vector length(const int);  // 第二个 length() 函数
复制代码

由于这两个函数的参数列表不同,导致在 Vector::angle 函数中调用 this->length() 时,编译器无法确定到底应该调用哪个函数。

要解决这个问题,你可以修改其中一个 length 函数的名称,或者给它们的参数列表做出一些改变,以确保它们在成员函数之间具有唯一的签名。

以下是一种可能的修复方法,为第二个 length 函数添加一个额外的参数,使其与原来的函数的签名不同:
  1. class Vector {
  2. public:
  3.     // ...
  4.     double length();                    // 第一个 length() 函数
  5.     Vector length(const int dimension); // 修改第二个 length() 函数的参数列表
  6.     // ...
  7. };
  8. double Vector::length() {
  9.     // 实现第一个 length() 函数的代码
  10. }
  11. Vector Vector::length(const int dimension) {
  12.     // 实现修改后的第二个 length() 函数的代码
  13. }
复制代码

通过这种方式,你可以为两个成员函数提供唯一的签名,从而消除编译器中的歧义。

希望这个解答能够帮助到你。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 05:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表