- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为 C++ 做作业,我对这门语言还很陌生。该作业是关于为具有复数根的函数实现牛顿拉夫森方法。我已经实现了代码。
我现在想测试我的代码,但我很难让我的主要功能正常工作,有一些我不理解的概念导致我错误的实现。我将不胜感激一些解释,以便我能更好地理解。谢谢。
这是我的代码示例:
复杂.h
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
private:
double r;
double i;
public:
Complex(double real, double imaginary);
friend Complex operator+(const Complex& c1, const Complex& c2);
friend ostream& operator<<(ostream& outs, const Complex& number);
};
复杂.cpp
#include "testComplex.h"
Complex::Complex(double real = 0.0, double imaginary = 0.0) : r(real), i(imaginary) {}
Complex operator+(const Complex& c1, const Complex& c2) {
Complex result;
result.r = c1.r + c2.r;
result.i = c1.i + c2.i;
return result;
}
主要.cpp
#include <iostream>
#include "testComplex.h"
using namespace std;
int main () {
Complex x;
Complex y;
Complex sum;
x = Complex(2, 4);
y = Complex(3, 0);
sum = x + y;
cout << "The sum (x + y) is: " << sum << endl;
return 0;
}
这是我收到的错误的一部分:
testComplexmain.cc: In function ‘int main()’:
testComplexmain.cc:8:10: error: no matching function for call to ‘Complex::Complex()’
testComplexmain.cc:8:10: note: candidates are:
testComplex.h:15:2: note: Complex::Complex(double, double)
testComplex.h:15:2: note: candidate expects 2 arguments, 0 provided
testComplex.h:8:7: note: Complex::Complex(const Complex&)
testComplex.h:8:7: note: candidate expects 1 argument, 0 provided
testComplexmain.cc:9:10: error: no matching function for call to ‘Complex::Complex()’
最佳答案
您没有默认构造函数。双参数构造函数可以作为一个参数使用,因为两个参数都是可选的;但是您必须将默认参数放在 header 中的声明中,而不是源文件中的定义中,以便它们可以从 main
中使用。
就个人而言,我会使用 std::complex
而不是重新发明它。
关于c++ - 复数类C++的主要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529351/
我有一个包含两列的文件,第一列包含纯实数,第二列包含 GNUPLOT 可接受的形式的复数,{a,b}(GNUPLOT 等同于 a + bi)。我想用第二列的绝对值绘制第一列,所以我尝试了这个: 使用
我正在使用 pandas,它可以非常有效地按照我需要的方式对数据进行排序/过滤。 此代码运行良好,直到我将最后一列更改为复数;现在我得到一个错误。 return self._cython_agg_ge
我如何让 Django 意识到 countries 的单数形式是 country 而不是 countrie 最佳答案 来自docs ,如果你有一个名为 num_countries 的模板变量,你可以这
我正在从事一个项目,该项目要求我获取输入、执行 DFT(离散傅立叶变换),然后从这些值中获取过零次数。 我编写了一个算法,但它使用复数,我不知道如何操作/执行计算。这是代码: #include #i
我如何在 C 中动态定义复数的三维数组,以便我可以在访问数组时方便的 [i] [j] [k] 符号中访问? 最佳答案 扩展 Bob__ 的 example将数组分配到堆上而不是堆栈上: #includ
我想在我的 Android 项目中使用复数形式。但是,我提供的值可以是浮点值。 例如,当设置 1.5 星时,我希望这个理解,它不是 1 星而是 1.5 星s。 %d star %d star
问题:这是我的虚数正则表达式: [0-9]{0,}\d\.[0-9]{0,}\d[i]|[0-9]{0,}\d[i] 它只取一个纯复数部分的虚数。 当我的解析器遇到例如String im = "2i"
我知道关键,现在我需要搜索 5000 多个用户数据库时产生的所有 结果。任何用户可能没有一个或多个位置,由 id 和 name 字段标识。因此,我需要一个数组中的结果,不仅仅是第一个/最后一个,而是所
Gidday cobbers/尊敬的同事们, 通过实现卡尔曼预测/校正的多目标跟踪,我在其他 SO 线程中看到建议的一般方法是简单地为每个目标提供一个卡尔曼滤波器 vector/数组。 即'多单目标卡
尽管在 C# naming convention for enum and matching property 上阅读了类似但不完全是我想要的内容,但我还是在问这个问题 我发现我倾向于以复数命名枚举,
我正在尝试使用 getQuantityString基于 Android 开发人员指南检索数量字符串(复数)的资源中的方法 Quantity string (plurals) 我得到的错误是 Error
我想用 C++ 处理复数。因此我包括了#include .现在我的问题是:我如何声明一个变量?(那么我们说的格式是什么:1 + i?) 提前致谢:-) 最佳答案 // 1 + 2i std::com
我有一个名为 agency_persons 的表,其中已有一些数据。我使用 php artisan 创建了一个模型: php artisan make:model AgencyPerson 现在,当我
在 C++ 中,复数 double 的一维数组的最大大小是多少(即 std::complex array[dimension] )? 如果我在 main 和 global 中声明数组,最大大小是否有区
我使用 Mustache 来模板化我的 javascript ajax 调用,这是我的数据和模板: {'joined':1} // ajax responde data json. var myTem
我有这么大的问题。我写了一个关于复数的程序。该程序读取和写入复数,将它们相加等。 他说我应该读一下 Why should I overload a C++ operator as a global f
我将在 java 中重复我必须做的事情,以我认为我需要思考的方式来完成这个任务。 (抱歉,我是编程新手)。 头等舱;为复数定义类。我发现这很容易,我的答案如下。 public class Comple
如何在新的 dev django 版本的管理站点上将某些模型名称从“Categorys”更改为“Categories”?在旧版本中(没有管理站点和管理模型)你可以这样做; http://www.the
我最近才发现一种通过 this video 生成毕达哥拉斯三元组的方法解释它,涉及使用高斯(复杂)整数。到目前为止,我已经设法编写了一个函数,该函数返回由每个高斯整数生成的勾股三元组列表,其中虚部小于
我决定尝试一下 complex.h,然后遇到了一个我认为非常奇怪的问题。 int mandelbrot(long double complex c, int lim) { long doubl
我是一名优秀的程序员,十分优秀!