-1

so recently I have been working on a truth table generator which generates an equation for a logic statement. So, I decided to use a 2D array to make things seem more clean and neat. However, as I run this code(Don't worry, int main and other functions exist too, I am posting only this part since it is the one that causes an error):

int calculate(size_t length) {

    int dimensionX = length + 2;
    int dimensionY = 5;
    calcArray = new string *[dimensionX];
    for (int i = 0; i < dimensionX; i++) {
        calcArray[i] = new string[dimensionY];
    }


    for (int i = 0; i < dimensionX; i++) {
        delete[] calcArray[i];
        delete[] calcArray;
    }

    return 0;
}

int count(string phrase, string calcArray[][]) {
    string statement(phrase);
    size_t ve = count(statement.begin(), statement.end(), 'A');
    size_t yada = count(statement.begin(), statement.end(), 'V');
    size_t ozel_yada = count(statement.begin(), statement.end(), 'XV');
    size_t ok = count(statement.begin(), statement.end(), '->');
    size_t cift_ok = count(statement.begin(), statement.end(), '<->');
    size_t complete = ve + yada + ozel_yada + ok + cift_ok;
    size_t column_number = complete;
    return complete;

    for (int i = 3; i < complete; i++) {
    int andNum = phrase.find('A');
    if (count (phrase.substr(andNum-3,6).begin(), phrase.substr(andNum - 3, 6).end(),'(') == 0){
        string calcArray[0][i] = phrase.substr(andNum - 1, 4);
    }
}

}

I get errors saying that:

An array may not have elements of this type( int count(string phrase, string calcArray[][])

cannot allocate an array of constant size 0 (string calcArray[0][i] = phrase.substr(andNum - 1, 4);)

An expression did not evaluate a constant (string calcArray[0][i] = phrase.substr(andNum - 1, 4);)

I would appreciate any help or explanation. Thanks

Fr1nge
  • 15
  • 1
  • 2
  • Where you have `string calcArray[0][i] = ...` (at the bottom of your code) are you sure you want the declaration instead of just `calcArray[0][i] = ...`? – EdmCoff Jan 10 '18 at 23:35
  • what do you mean? – Fr1nge Jan 10 '18 at 23:39
  • I don't understand why you are redeclaring calcArray in your last line of posted code, although I guess it's kind of a moot point. – EdmCoff Jan 10 '18 at 23:43
  • So, since this is a dinamic array, at start I used string **calcArray = 0; so will it work if I just say string **calcArray at the beggining of the count function – Fr1nge Jan 10 '18 at 23:46
  • No, sorry for the confusion. I am pointing out what I think is a second error in your posted code. For your actual question, I recommend looking at https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – EdmCoff Jan 11 '18 at 00:00

1 Answers1

1
string calcArray[][]

is not a valid declaration for a function parameter. When you use a multi-dimensional array, all dimensions except the first one must be specified. E.g.

string calcArray[][10]


string anotherArray[][10][2]

If the arrays are dynamic, you can use std::vector<std::vector<std::string>>.

std::vector<std::vector<std::string>>& calcArray
R Sahu
  • 196,807
  • 13
  • 136
  • 247