简体   繁体   中英

I am not sure why the code isn't running, it throws an error while compiling saying it is unable to locate PrintArray()

int main()
{
    const char arr[][5] = {{ 'H','u','g','o','\0' },{'h','i'}};
    printArray(arr, 2, 5);

}

void printArray(const char arr[], int size)

{
    for(int i=0;i<size;i++)
    {
        cout << arr[i];
    }
    cout << endl;
}

void printArray(const char arr[][SIZE], int rows, int cols)
{   char temp[cols];

        for(int i=0;i<rows;i++)
        {char temp[cols];
            for(int j=0;j<cols;j++)
            {
                temp[j]=arr[i][j];
            }
            printArray(temp,cols);

        }

}

The code is meant to print the double dimensional array line by line. And it's supposed to overload printArray();

Not sure why it does not work, everything seems to be fine.

error message: No matching function for call to 'printArray' at this line printArray(arr, 2, 5);

You might be facing a behaviour of C++ compiler, namely order of declaration. Basically, your main() function try to call to yet undefined printArray function. By reordering the definition, the function call will work: https://onlinegdb.com/r1jMt9d0r .

void printArray(const char arr[], int size)
{
    for(int i=0;i<size;i++)
    {
        cout << arr[i];
    }
    cout << endl;
}

void printArray(const char arr[][SIZE], int rows, int cols)
{   char temp[cols];

        for(int i=0;i<rows;i++)
        {char temp[cols];
            for(int j=0;j<cols;j++)
            {
                temp[j]=arr[i][j];
            }
            printArray(temp,cols);

        }

}

int main()
{
    const char arr[][5] = {{ 'H','u','g','o','\0' },{'h','i'}};
    printArray(arr, 2, 5);

}

This question actually have been asked, but you might not able to search the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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