簡體   English   中英

C++ 無法將參數“1”轉換為“long”的“long double”轉換為“long double*”

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

我對編程和 C++ 很陌生,我有一個作業,我需要使用 Cramer 規則來解決系統問題。 一切都很好,而且有效,但我必須更好地利用函數。 每次我使用函數時都會遇到轉換錯誤,通常我可以弄清楚,但我收到一條錯誤消息“第 51 行錯誤:無法將 'long double' 轉換為 'long double*' 以將參數 '1' 轉換為 'long double Cramer_3x3(long double*, long double (*)[3], int)'"。

這是我的代碼:

#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;
}

我正在使用 GNU GCC 編譯器。

幫助將不勝感激,我看過類似的問題,但沒有多大幫助。

您將數組的特定(未定義)索引傳遞給函數而不是數組本身

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

應該

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM