473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function Compile Error

17
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:163: error: invalid conversion from ‘int (*)[1]’ to ‘int'"?

Expand|Select|Wrap|Line Numbers
  1. int initcreate() {
  2.     int initiative[(MAXUNITS-1)][1];
  3.     int unitcount = 0;
  4.     for (int i = 0; i < (BOARDX-1); i++) {
  5.         for (int j = 0; j < (BOARDY-1); j++) {
  6.             if (forces[i][j] != 0) {
  7.                 initiative[unitcount][0] = i;
  8.                 initiative[unitcount][1] = j;
  9.                 unitcount++;
  10.             }
  11.         }
  12.     }
  13.     return initiative;
  14. }
Apr 7 '07 #1
6 1329
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?

Expand|Select|Wrap|Line Numbers
  1. int initcreate() {
  2.     int initiative[(MAXUNITS-1)][2];
  3.     int unitcount = 0;
  4.     for (int i = 0; i < (BOARDX-1); i++) {
  5.         for (int j = 0; j < (BOARDY-1); j++) {
  6.             if (forces[i][j] != 0) {
  7.                 initiative[unitcount][0] = i;
  8.                 initiative[unitcount][1] = j;
  9.                 unitcount++;
  10.             }
  11.         }
  12.     }
  13.     return initiative;
  14. }
[/quote]

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:163: error: invalid conversion from ‘int (*)[1]’ to ‘int'"?

Expand|Select|Wrap|Line Numbers
  1. int initcreate() {
  2.     int initiative[(MAXUNITS-1)][1];
  3.     int unitcount = 0;
  4.     for (int i = 0; i < (BOARDX-1); i++) {
  5.         for (int j = 0; j < (BOARDY-1); j++) {
  6.             if (forces[i][j] != 0) {
  7.                 initiative[unitcount][0] = i;
  8.                 initiative[unitcount][1] = j;
  9.                 unitcount++;
  10.             }
  11.         }
  12.     }
  13.     return initiative;
  14. }
Apr 7 '07 #2
nmadct
83 Expert
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!
Apr 7 '07 #3
kidko
17
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
Expand|Select|Wrap|Line Numbers
  1. int initiativelist[(MAXUNITS-1)][2]; // MAXUNITS is declared outside of all functions in functions.h
  2. initiativelist = initcreate();
Should I change my code to something resembling this:
Expand|Select|Wrap|Line Numbers
  1. // MAIN.CPP
  2. int *initiativelist[(MAXUNITS-1)][2]; // MAXUNITS is declared outside of all functions in functions.h
  3. initiativelist = initcreate(initiativelist);
  4.  
  5. // FUNCTIONS.H
  6. int* initcreate(int *array) {
  7.     int unitcount = 0;
  8.     for (int i = 0; i < (BOARDX-1); i++) {
  9.         for (int j = 0; j < (BOARDY-1); j++) {
  10.             if (forces[i][j] != 0) {
  11.                 array[unitcount][0] = i;
  12.                 array[unitcount][1] = j;
  13.                 unitcount++;
  14.             }
  15.         }
  16.     }
  17.     return *array;
  18. }
Apr 8 '07 #4
nmadct
83 Expert
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:

Expand|Select|Wrap|Line Numbers
  1. struct twoelements { int x; int y; };
  2. ...
  3.    struct twoelements myarray[ARRAY_LEN];
  4.    dostuff(myarray);
  5. ...
  6. void dostuff(struct twoelements * myargument) {
  7.    ...
  8.    myargument[2].x = 4;
  9.    myargument[2].y = 8;
  10.    ...
  11. }
  12.  
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.
Apr 8 '07 #5
kidko
17
Structs... I totally forgot about those, I think they'll work very well. Thank you very much for your help in answering my question!

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:

Expand|Select|Wrap|Line Numbers
  1. struct twoelements { int x; int y; };
  2. ...
  3.    struct twoelements myarray[ARRAY_LEN];
  4.    dostuff(myarray);
  5. ...
  6. void dostuff(struct twoelements * myargument) {
  7.    ...
  8.    myargument[2].x = 4;
  9.    myargument[2].y = 8;
  10.    ...
  11. }
  12.  
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.
Apr 9 '07 #6
nmadct
83 Expert
Great, I'm glad that works for you!
Apr 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Dario | last post by:
This is a technical C++ post regarding the Microsoft runtime error R6025 Pure Virtual Function Call that sometime occurs in programs compiled with Microsoft Visual C++ 6.0. Please consider the...
6
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
4
by: twelvetone | last post by:
Can anyone explain why this file won't compile? The errors I'm getting in VS.Net 2003 are below. ============================ c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(24): error C3861: 'str':...
10
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
5
by: Jeff Newman | last post by:
Hi all, I am trying to figure out what is causing the compile error in the following example. I have two functions which are identical (except one is templated), both trying to call a template...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.