错误:没有operator =匹配这些操作数

时间:2015-11-01 05:01:39

标签: c++ fractions

我怎么做fractionAry[i] = new Fraction(num1, denom1)它有效并将创建的分数存储到i的索引中。

但是当我执行fractionAry[i][j] = new Fraction(num1, denom1)时,编译器会给出一个错误,指出没有运算符匹配这些操作数?

如果我想将分数存储到j,我该怎么做?我不完全确定如何将分数存储到分数数组的数组中......

这是我的代码,感谢您提前获得的帮助。

void createArray() {
    Fraction** fractionAry;
    int aryCount;
    int arySize;
    int i, j;
    int num1, denom1;

    cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
    cin >> aryCount;

    if (aryCount > 0) {
        fractionAry = new Fraction*[aryCount];
        for (i = 0; i < aryCount; i++) {
            cout << "\nCreating array index # " << i
                << "\n  How many fractions(s)? ";
            cin >> arySize;

            if (arySize > 0) {
                fractionAry[i] = new Fraction[arySize + 1];
                fractionAry[i][0] = arySize;

                for (j = 1; j < arySize + 1; j++) {
                    cout << "\n    Enter the numerator: ";
                    cin >> num1;
                    cout << "    Enter the denominator: ";
                    cin >> denom1;
                    while (denom1 == 0) {
                        cout << "\nCan't set to 0! Enter a new denominator: ";
                        cin >> denom1;
                    }
                    fractionAry[i] = new Fraction(num1, denom1);

                    // fractionAry[i][j] = new Fraction(num1, denom1); I would like to do this instead
                }

                cout << "\nFor array index #" << i << endl;
                for (j = 0; j < arySize + 1; j++) {
                    cout << "  Element index #" << j << " : " << *(*(fractionAry + i) + j) << endl;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用fractionAry [i] [j] =分数(...)而不是使用fractionAry [i] [j] = new Fraction(...)。 fractionAry [i] [j]的类型是Fraction not Fraction *,这是新运算符返回的类型。

void createArray() {
    Fraction** fractionAry;
    int aryCount;
    int arySize;
    int i, j;
    int num1, denom1;

    cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
    cin >> aryCount;
    if (aryCount < 1)
    {
        //print error message or exit function
    }
    fractionAry = new Fraction*[aryCount];
    for (i = 0; i < aryCount; i++) 
    {
        cout << "\nCreating array index # " << i
            << "\n  How many fractions(s)? ";
        cin >> arySize;
        if (arySize < 1)
        {
            //Print error message decrement i and continue or exit function
        }
        fractionAry[i] = new Fraction[arySize + 1];
        //I don't understand the purpose of assigning the below value or making the array one size bigger
        fractionAry[i][0] = arySize;
        ///
        for (j = 1; j < arySize + 1; j++)
        {
            cout << "\n    Enter the numerator: ";
            cin >> num1;
            cout << "    Enter the denominator: ";
            cin >> denom1;
            while (denom1 == 0)
            {
                cout << "\nCan't set to 0! Enter a new denominator: ";
                cin >> denom1;
            }
            fractionAry[i][j] = Fraction(num1, denom1);
        }
        cout << "\nFor array index #" << i << endl;
        for (j = 0; j < arySize + 1; j++) 
        {
            cout << "  Element index #" << j << " : " << fractionAry[i][j] << endl;
        }

    }
}