简体   繁体   English

C++ 无法将参数“1”转换为“long”的“long double”转换为“long double*”

[英]C++ cannot convert 'long double' to 'long double*' for argument '1' to 'long

I'm very new to programming and C++, and I have an assignment where I need to use Cramer's rule to solve a system.我对编程和 C++ 很陌生,我有一个作业,我需要使用 Cramer 规则来解决系统问题。 That's all fine, and it worked, but I had to utilise functions better.一切都很好,而且有效,但我必须更好地利用函数。 Every time I use functions I run into conversion errors, usually I can figure it out, but I got an error saying "line 51 error: cannot convert 'long double' to 'long double*' for argument '1' to 'long double Cramer_3x3(long double*, long double (*)[3], int)'".每次我使用函数时都会遇到转换错误,通常我可以弄清楚,但我收到一条错误消息“第 51 行错误:无法将 'long double' 转换为 'long double*' 以将参数 '1' 转换为 'long double Cramer_3x3(long double*, long double (*)[3], int)'"。

Here is my code:这是我的代码:

#include <iostream>

using namespace std;

long double Determinant_3x3(long double matrix_3x3[3][3])
{
    long double determinant;
    determinant = matrix_3x3[0][0] * matrix_3x3[1][1] * matrix_3x3[2][2] + matrix_3x3[0][1] * matrix_3x3[1][2] * matrix_3x3[2][0] + matrix_3x3[0][2] * matrix_3x3[1][0] * matrix_3x3[2][1] - matrix_3x3[0][2] * matrix_3x3[1][1] * matrix_3x3[2][0] - matrix_3x3[0][1] * matrix_3x3[1][0] * matrix_3x3[2][2] - matrix_3x3[0][0] * matrix_3x3[1][2] * matrix_3x3[2][1];
    return determinant;
}

long double Cramer_3x3(long double d[3], long double matrix_3x3[3][3], int step)
{
long double result;

if(step == 0){
    long double matrix_3x3d[3][3] = {d[0], matrix_3x3[0][1], matrix_3x3[0][2], d[1], matrix_3x3[1][1], matrix_3x3[1][2], d[2], matrix_3x3[2][1], matrix_3x3[2][2]};
    result = Determinant_3x3(matrix_3x3d)/Determinant_3x3(matrix_3x3);
}

else if(step == 1){
    long double matrix_3x3d[3][3] = {matrix_3x3[0][0], d[0], matrix_3x3[0][2], matrix_3x3[1][0], d[1], matrix_3x3[1][2], matrix_3x3[2][0], d[2], matrix_3x3[2][2]};
    result = Determinant_3x3(matrix_3x3d)/Determinant_3x3(matrix_3x3);
}

else{
    long double matrix_3x3d[3][3] = {matrix_3x3[0][0], matrix_3x3[0][1], d[0], matrix_3x3[1][0], matrix_3x3[1][1], d[1], matrix_3x3[2][0], matrix_3x3[2][1], d[2]};
    result = Determinant_3x3(matrix_3x3d)/Determinant_3x3(matrix_3x3);
}

return result;
}

int main()
{
long double d[3], matrix_3x3[3][3], result[3];

cout << "Voer een 3x3 matrix in.\n";
for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        cin >> matrix_3x3[i][j];
    }
}
cout << "Voer d1, d2 en d3 in.\n";
for(int k = 0; k < 3; k++){
    cin >> d[k];
    result[k] = Cramer_3x3(d[3], matrix_3x3[3][3], k); //line 51, where the error happens
}



cout << "Het ingevoerde stelsel ziet er zo uit.\n" << matrix_3x3[0][0] << "x + " << matrix_3x3[0][1] << "y + " << matrix_3x3[0][2] << "z = " << d[0] << endl << matrix_3x3[1][0] << "x + " << matrix_3x3[1][1] << "y + " << matrix_3x3[1][2] << "z = " << d[1] << endl << matrix_3x3[2][0] << "x + " << matrix_3x3[2][1] << "y + " << matrix_3x3[2][2] << "z = " << d[2] << endl << "x = " << result[0] << "\ny = " << result[1] << "\nz = " << result[2] << endl;

return 0;
}

I'm using the GNU GCC compiler.我正在使用 GNU GCC 编译器。

Help would be greatly appreciated, I have looked at similar questions, but it didn't help much.帮助将不胜感激,我看过类似的问题,但没有多大帮助。

You're passing a specific (undefined) index of your array to your function instead of the array itself您将数组的特定(未定义)索引传递给函数而不是数组本身

result[k] = Cramer_3x3(d[3], matrix_3x3[3][3], k)

Should be应该

result[k] = Cramer_3x3(d, matrix_3x3, k)

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

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