Function Compile Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kidko
    New Member
    • Apr 2007
    • 17

    #1

    Function Compile Error

    Hi... I'm working on a small, text-based game, compiling under Kubuntu 6.10 (Edgy Eft) with G++ 4.1.2. Up until I added this function (and its corresponding call in main.cpp), it would compile correctly. Could anybody tell me why I'm getting the error "functions.h:16 3: error: invalid conversion from ‘int (*)[1]’ to ‘int'"?

    Code:
    int initcreate() {
    	int initiative[(MAXUNITS-1)][1];
    	int unitcount = 0;
    	for (int i = 0; i < (BOARDX-1); i++) {
    		for (int j = 0; j < (BOARDY-1); j++) {
    			if (forces[i][j] != 0) {
    				initiative[unitcount][0] = i;
    				initiative[unitcount][1] = j;
    				unitcount++;
    			}
    		}
    	}
    	return initiative;
    }
  • tavianator
    New Member
    • Dec 2006
    • 38

    #2
    This code should me correct (note the [2] in the second line, replacing [1]), as long as forces is a two-dimensional array. Where specifically, in the function, is the error?

    Code:
    int initcreate() {
    	int initiative[(MAXUNITS-1)][2];
    	int unitcount = 0;
    	for (int i = 0; i < (BOARDX-1); i++) {
    		for (int j = 0; j < (BOARDY-1); j++) {
    			if (forces[i][j] != 0) {
    				initiative[unitcount][0] = i;
    				initiative[unitcount][1] = j;
    				unitcount++;
    			}
    		}
    	}
    	return initiative;
    }
    [/QUOTE]

    Originally posted by kidko
    Hi... I'm working on a small, text-based game, compiling under Kubuntu 6.10 (Edgy Eft) with G++ 4.1.2. Up until I added this function (and its corresponding call in main.cpp), it would compile correctly. Could anybody tell me why I'm getting the error "functions.h:16 3: error: invalid conversion from ‘int (*)[1]’ to ‘int'"?

    Code:
    int initcreate() {
    	int initiative[(MAXUNITS-1)][1];
    	int unitcount = 0;
    	for (int i = 0; i < (BOARDX-1); i++) {
    		for (int j = 0; j < (BOARDY-1); j++) {
    			if (forces[i][j] != 0) {
    				initiative[unitcount][0] = i;
    				initiative[unitcount][1] = j;
    				unitcount++;
    			}
    		}
    	}
    	return initiative;
    }

    Comment

    • nmadct
      Recognized Expert New Member
      • Jan 2007
      • 83

      #3
      Your function is declared as type int, but you are returning a pointer to a 2-dimensional array of int. Also, the array you're returning a pointer to is on the stack, so it won't exist anymore after the function returns!

      Comment

      • kidko
        New Member
        • Apr 2007
        • 17

        #4
        The error message points to the last line, return initiative;.

        How would I return the contents of the array itself, if in my main.cpp I had something like
        Code:
        int initiativelist[(MAXUNITS-1)][2]; // MAXUNITS is declared outside of all functions in functions.h
        initiativelist = initcreate();
        Should I change my code to something resembling this:
        Code:
        // MAIN.CPP
        int *initiativelist[(MAXUNITS-1)][2]; // MAXUNITS is declared outside of all functions in functions.h
        initiativelist = initcreate(initiativelist);
        
        // FUNCTIONS.H
        int* initcreate(int *array) {
        	int unitcount = 0;
        	for (int i = 0; i < (BOARDX-1); i++) {
        		for (int j = 0; j < (BOARDY-1); j++) {
        			if (forces[i][j] != 0) {
        				array[unitcount][0] = i;
        				array[unitcount][1] = j;
        				unitcount++;
        			}
        		}
        	}
        	return *array;
        }

        Comment

        • nmadct
          Recognized Expert New Member
          • Jan 2007
          • 83

          #5
          Yes, passing an existing array to the function as an argument is probably the best approach. This gives the calling function the most flexibility and control. However, what you're doing in this latest code is passing in a 1-dimensional array and using it as a 2-dimensional array. That won't work.

          Since you have only 2 dimensions, an alternative way to set up the data structure would be to create a struct with 2 members, and pass an array of that struct. Something like this:

          Code:
          struct twoelements { int x; int y; };
          ...
             struct twoelements myarray[ARRAY_LEN];
             dostuff(myarray);
          ...
          void dostuff(struct twoelements * myargument) {
             ...
             myargument[2].x = 4;
             myargument[2].y = 8;
             ...
          }
          If you decide to use multi-dimensional arrays you have to be very careful about how you declare them when you're passing them around as function arguments, so often it's best to just avoid them altogether.

          Comment

          • kidko
            New Member
            • Apr 2007
            • 17

            #6
            Structs... I totally forgot about those, I think they'll work very well. Thank you very much for your help in answering my question!

            Originally posted by nmadct
            Yes, passing an existing array to the function as an argument is probably the best approach. This gives the calling function the most flexibility and control. However, what you're doing in this latest code is passing in a 1-dimensional array and using it as a 2-dimensional array. That won't work.

            Since you have only 2 dimensions, an alternative way to set up the data structure would be to create a struct with 2 members, and pass an array of that struct. Something like this:

            Code:
            struct twoelements { int x; int y; };
            ...
               struct twoelements myarray[ARRAY_LEN];
               dostuff(myarray);
            ...
            void dostuff(struct twoelements * myargument) {
               ...
               myargument[2].x = 4;
               myargument[2].y = 8;
               ...
            }
            If you decide to use multi-dimensional arrays you have to be very careful about how you declare them when you're passing them around as function arguments, so often it's best to just avoid them altogether.

            Comment

            • nmadct
              Recognized Expert New Member
              • Jan 2007
              • 83

              #7
              Great, I'm glad that works for you!

              Comment

              Working...