I am tring to compile this simple string array. I am getting some struct error. "Cannot pass object of non-POD type in line 63. which is "printf("%14d%10d\n",array[1], array[0]);"

please help


#include <stdlib.h>
#include <time.h>
#include <string>

#define row 15
#define col 2

using namespace std;

void PrintArray(char *Title, string array[row][col]);
void Shuffle(string m[row][col]);

int main(void)
{
    string array[row][col] = {"the farmer drove to the city",
			"the brakes were slipping",
			"the road got very wet",
 			"water ran down the hill",
			"potatoes spilled everywhere",
			"she laid an egg",
			"the feathers went flying",
			"the duck went straight into the air",
			"the dog ran quickly after it",
			"everything was planted last spring",
			"the farmers jumped in",
			"april is a good time",
			"rocks rolled everywhere",
			"big bumps shook the cargo",
			"the clock seems to tick faster"};;



    /* initialize random generator */
    srand( time(NULL));

    /* fill the array with element # and random number */

    for (int i=0; i<row; i++)
    {
       array[i][1] = rand();
    }

    PrintArray("Original Data",array);
    Shuffle(array);

    PrintArray("Shuffled Data",array);


     return 0;
}


void PrintArray(char *Title, string array[row][col])
{
/* print the contents */
     printf("\t%s\n",Title);

     //printf("\tRandom\tElement\n");
     printf("%14s%10s\n","Random","Element");

     for (int i=0; i<row; i++)
     {
         printf("%14d%10d\n",array[i][1], array[i][0]);
     }

   /*add some white space*/
    puts("\n\n");
}

void Shuffle(int a[row][col])
{

    /*  column 1 is random number, column 0 is element*/
    int i,j,t;
    /* the row for loop */
     for (i=0; i < row-1; i++)
       /* the column for loop */
        for (j=0; j < row-i-1; j++)

            if (a[j][1] > a[j+1][1])
            {
                t=a[j][1];
                a[j][1]=a[j+1][1];
                a[j+1][1]=t;

                t=a[j][0];
                a[j][0]=a[j+1][0];
                a[j+1][0]=t;
            }
}

that error message means that you are attempting to pass a std::string object to printf, but printf() required an integer -- "%d" is an integer, not a string. If all you want is the first character of a string then you need to access it like this: array[i][1][0] , otherwise if you want to pass the entire string then use "%s" and array[i][1].c_str()

now i get "undefined reference to `Shuffle(std::string (*) [2])'
collect2: ld returned 1 exit status
"
but it seems to compile. I am using code blocks

You didn't code any function with that parameter. The function you coded takes an array of ints, not an array of std::string. Consequently, in c++, they are two different functions.

This article has been dead for over six months. Start a new discussion instead.