- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Integer
类,它应该模拟一个整数 mod n。因此,它具有如下构造函数:
Integer::Integer(int x)
: m(x), n(0)
{
}
Integer::Integer(int x, int y)
: n(y), m(x)
{
// if this->n greater than 1
if (this->n > 1)
{
// mod this->m by this->n
this->m %= this->n;
// if this->m is negative
if (this->m < 0)
{
// add this->n to it
this->m += this->n;
}
}
}
还有Integer::inverse()
、Integer::pow(int)
,这是本题完整性所需要的:
Integer Integer::inverse()
{
// Extended Euclidean Algorithm
int t = 0,
r = this->n,
newT = 1,
newR = this->m;
while (newR != 0)
{
int quotient = r / newR,
tCopy = t,
rCopy = r,
newTCopy = newT,
newRCopy = newR;
t = newT;
newT = tCopy - quotient * newTCopy;
r = newR;
newR = rCopy - quotient * newRCopy;
}
if (r > 1)
{
throw Integer(-1);
}
if (t < 0) t = t + this->n;
return Integer(t, this->n);
}
Integer Integer::squared()
{
return Integer(this->m * this->m, this->n);
}
Integer Integer::pow(int x)
{
// if x less than 0, return this->inverse().pow(-x)
if (x < 0) return this->inverse().pow(-x);
// if x is 0, return Integer(1)
if (x == 0) return Integer(1, this->n);
// if x is 1, return *this
if (x == 1) return *this;
// if x is 2, return this->squared()
if (x == 2) return this->squared();
// if x greater than 2
if (x > 2)
{
// if x is even
if (x % 2 == 0)
{
// return this->pow(x/2).squared()
return this->pow(x/2).squared();
}
// return this->pow(x/2).squared() * (*this)
return this->pow(x/2).squared() * (*this);
}
}
我遇到的问题是当我去实现 Integer::isQuadraticResidue() const
时:
bool Integer::isQuadraticResidue() const
{
// if this->n is zero
if (this->n == 0)
{
// this doesn't belong to Integers mod anything. check for perfect square instead
double baseSquareRoot = std::sqrt((double)this->m);
return (baseSquareRoot == (double)((int)baseSquareRoot));
}
// this is quadratic residue iff this->pow((this->n + 1) / 2) == Integer(1, this->n)
return (this->pow((n + 1) / 2).m == 1);
}
我收到以下错误:错误:将“const Integer”作为“Integer Integer::pow(int)”的“this”参数传递会丢弃限定符
。我认为它与末尾的 const
有关。做什么?
编辑:类头文件看起来像:
#ifndef INTEGER_H
#define INTEGER_H
#include <iostream>
class Integer
{
public:
Integer(int);
Integer(int, int);
// functions
Integer inverse();
Integer squared();
Integer pow(int);
bool isQuadraticResidue() const;
Integer sqrt();
private:
int m, n;
};
#endif
最佳答案
这是 const
正确性的问题,其中 const
函数正在尝试调用非 const
函数。
// ...
bool Integer::isQuadraticResidue() const;
Integer Integer::pow(int x);
// ....
在这种情况下,this
是pow()
中的一个Integer*
,一个const Integer*
在 isQuadraticResidue()
中;这意味着 pow()
可以调用 isQuadraticResidue()
,因为添加 CV 限定符是合法的,但反之则不行(因为 pow()
将不得不接受 this
作为非限定 Integer*
,失去 const
限定符)。
这是不允许的,因为允许它意味着 isQuadraticResidue()
打破了它不会直接或间接修改实例的保证。虽然它本身不改变状态,但它假设 pow()
确实改变了状态,因为 pow()
也不是 const
(因此不保证不改变状态)。因此,isQuadraticResidue()
无法调用 pow()
,因为这样做可能会破坏其保证。
考虑到这一点,这个问题有两种解决方案。
isQuadraticResidue()
中移除 const
。这会天真地解决问题,但不推荐这样做,因为那样您将无法获得 const
正确性的好处。将所有不修改Integer
逻辑状态的成员函数也设为const
。这将需要更多的努力,但总体上更安全。因为它们可以在 const Integer
上调用,就像在 Integer
上调用一样,所以您可以将实例作为 const< 传递
每当它们不需要修改时,为您提供更大程度的安全性。
Integer Integer::inverse() const;
Integer Integer::squared() const;
Integer Integer::pow() const;
这需要在函数的原型(prototype)及其定义中进行更改。
关于c++ - 错误 : passing ‘const Integer’ as ‘this’ argument of ‘Integer Integer::pow(int)’ discards qualifiers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886006/
我在提交更改时不小心单击了放弃。所以我丢弃了整个文件,但我只想丢弃一些大块头。但是我没有点击提交,我没有点击取消。 但是现在我在我的文件中看不到我的代码。 我该怎么做才能撤消该死的丢弃? 提前致谢 最
我们有一个变更集,开发人员已在其中 checkin 了对源分支和目标分支的更改,许多更改包括两个分支中的重命名。从源分支到目标分支的变更集合并正常,但变更集仍保留在要合并的变更集列表中。 当我现在尝试
设置: 我有一个表单和一个“提交”按钮。理想情况下,用户应该填写表格,单击“提交”,然后离开选项卡。如果他尝试离开选项卡而不保存更改,我需要通过 3 个选项提醒他: 保存 放弃:放弃表单数据更改,并保
从 C# 7 开始,我们可以使用丢弃 _ 来丢弃未使用的变量。我用它做的事情之一是即发即弃任务。 考虑这个方法: public Task Example() { // Do some fire
昨晚不经意间做了一件很蠢的事。我已经在 iPad 项目上工作了几个月并且(甚至更愚蠢)没有备份。通过在 XCODE 中右键单击该项目,我错误地单击了“放弃更改” (愚蠢再次占据上风)自最初以来我没有任
我有一个修改和实例化的 Action ,然后链接到另一个 Action 。如果实例上有错误,我想忽略所做的更改。但是,throws()方法不起作用。状态更改始终保持不变。我究竟做错了什么? def r
我刚刚意识到 GL_ALPHA_TEST自 OpenGL 3.0 起已被弃用,因此我无法将它与 glEnable() 一起使用。我已经尝试快速搜索谷歌现在如何替换它(以及为什么它被弃用),但未能找到这
使用 CPP map ,从 eclipse 编辑器获取警告信息。 Point find_shortest_node(const vector &openset,const map &f_score
我不理解以下示例中 x3 的行为(取自更大的语法)。 当然,语法有点奇怪,但大致实现了 (lal)?()? .当第二组不存在时,默认为 .我不明白为什么要输入 "lal"我得到 defaultcha
这个问题在这里已经有了答案: Is there a way to make bash job control quiet? (5 个答案) 关闭 3 年前。 我正在运行这个命令: nohup goo
当我尝试在 LWJGL 3 中实现纹理的透明度时,模型似乎以完全透明的方式渲染,因此根本不可见。当我删除支票时,它工作正常。有什么线索可以解释为什么吗? 下面是检查 alpha 分量的着色器代码: #
使用 GCC 和 C99 模式,我有一个函数声明为: void func(float *X); 当我调用该函数时,我使用了一个可变数组 Y: volatile float Y[2]; int main
本文整理了Java中org.jruby.parser.YYDebug.discard()方法的一些代码示例,展示了YYDebug.discard()的具体用法。这些代码示例主要来源于Github/St
可以使用加号按钮“暂存此行”轻松暂存行。 有“Discard Hunk”和“Stage Hunk”符号。 但是如何“丢弃行”呢?有什么快捷方式可以将行旁边的加号按钮更改为减号按钮吗? 最佳答案 您可以
我已经使用 jQuery.ajax() 方法构建了实时搜索。在每个 keyup 事件中,它都会从服务器接收新的结果数据。 问题是,当我打字速度非常快时,例如“foobar”和“fooba”的GET请求
我有一个看起来像这样的图形日志: (snip) | | | o 1) Other Dev: Commit | | \ o | | 2) Me: Commit / | | | |
我只是想将 mesos 版本从 1.0.3 升级到 1.3.1。 Chronos 调度程序能够通过 mesos 调度 JOB。该作业运行良好并且能够看到 mesos stdout 日志。但是,仍然在
我有一个要调试的 BufferedInputStream。为此,我使用此方法(Log.d 和 Log.wtf 只是 Android 特定的日志记录工具,除此之外其行为不应与纯 Java 不同):
假设它是使用“放弃 merge 中的所有更改...”选项执行 merge 的,但意外。 这个错误的提交已经提交到“中央”存储库中。 如果我遇到这种情况如何处理需要以正确的方式 merge 分支的变化?
我尝试将数据从 Firebase 数据库检索到我的回收站 View ,无论我尝试什么,我都会收到此错误。我该如何解决这个问题? 在我的模拟器中,Google Play 服务版本是:11.5.80 我没
我是一名优秀的程序员,十分优秀!