C++ 如何使用断言对阵列进行测试?

C++ 如何使用断言对阵列进行测试?,c++,algorithm,matrix,C++,Algorithm,Matrix,我正在尝试为一个程序编写一个测试,该程序将矩阵a和X的乘积添加到矩阵Y。 但我得到了一个错误: "Identifier is required" 我无法解决或找到解决这个问题的方法,所以我在这里寻求帮助 起初,我认为问题在于我将它与错误的数组进行了比较。然后我试着传递其他论点。将我的代码分解成几个函数。但还是什么也没发生 #include<iostream> #include<cassert> using namespace std; void axpy(int n

我正在尝试为一个程序编写一个测试,该程序将矩阵
a
X
的乘积添加到矩阵
Y
。 但我得到了一个错误:

"Identifier is required"
我无法解决或找到解决这个问题的方法,所以我在这里寻求帮助

起初,我认为问题在于我将它与错误的数组进行了比较。然后我试着传递其他论点。将我的代码分解成几个函数。但还是什么也没发生

#include<iostream>
#include<cassert>

using namespace std;

void axpy(int n, int m, int k, int **A, int **X, int **Y)
{
    int i, j, q;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            for (q = 0; q < k; q++)
            {
                Y[i][j] += A[i][q] * X[q][j];
            }
        }
    }
    cout << "Product of matrices\n";
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
            cout << Y[i][j] << "  ";
        cout << "\n";
    }
}

void TestAxpy()
{
    int P[2][2] = { {13,11},{27,21} };
    assert(axpy(2,2,2,[1,2][3,4],[4,3][2,1],[5,6][7,8]) == P);
}

int main()
{
    int n, m, k, i, j, q;

    cout << "Enter number of rows of matrix X and columns of matrix A: ";
    cin >> k;
    cout << "Enter number of rows of matrix A and Y: ";
    cin >> n;
    cout << "Enter number of columns of matrix X and Y: ";
    cin >> m;

    int **A = new int *[k];

    for (i = 0; i < k; i++)
        A[i] = new int[n];

    int **X = new int *[m];

    for (i = 0; i < m; i++)
        X[i] = new int[k];

    int **Y = new int *[m];

    for (i = 0; i < m; i++)
        Y[i] = new int[n];


    cout << "Enter elements of matrix A: ";
    for (i = 0; i < n; i++)
        for (j = 0; j < k; j++)
            cin >> A[i][j];
    cout << "Enter elements of matrix X: ";
    for (i = 0; i < k; i++)
        for (j = 0; j < m; j++)
            cin >> X[i][j];
    cout << "Enter elements of matrix Y: ";
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            cin >> Y[i][j];
    axpy(n, m, k, A, X, Y);
    TestAxpy();
    system("pause");
    return 0;
}

<>这似乎是C和C++的混合体。在C++中,你需要使用原始的“C”数组,几乎总是<代码> STD::vector < /COD>或<代码> STD::数组< /C> >将是更好的选择。boost库中还有一个矩阵,它可以准确地存储您需要的内容

就您的特定代码而言,有两个问题:

  • 指向指针(**)的指针与二维数组不同。它们是两层间接寻址。第一个是指向内存中存储第二层的位置的指针。请参阅下面的内容,了解调用axpy所需的工作方式。再次强烈建议查看std::vector或boost库
  • 对于C数组,“==”运算符不能以这种方式工作。您需要指定希望如何进行比较。正如所写的,它最多只能比较内存地址,但更可能会产生错误

  • axpy(2,2,2[1,2][3,4],[4,3][2,1],[5,6][7,8])
    毫无意义。我甚至猜不出这是什么意思。通常情况下:指针不是数组。数组不是指针。
    new
    newnewnewnewnewnew-
    std::vector
    是你的朋友
    Enter number of rows of matrix X and columns of matrix A: 2
    Enter number of rows of matrix A and Y: 2
    Enter number of columns of matrix X and Y: 2
    Enter elements of matrix A: 1 2 3 4
    Enter elements of matrix X: 4 3 2 1
    Enter elements of matrix Y: 5 6 7 8
    
    void TestAxpy()
    {
        int P[2][2] = { {13,11},{27,21} };
        int A1[2] = {1,2};
        int A2[2] = {3,4};
        int* A[2] = { A1, A2 };
    
        int X1[2] = {4,3};
        int X2[2] = {2,1};
        int *X[2] = { X1, X2 };
    
        int Y1[2];
        int Y2[2];
        int *Y[2] = {Y1, Y2 };    
    
        axpy(2,2,2,A,X,Y);
        //assert(Y == P); //C++ doesn't know how to do this.
    }