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'"? - 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;
-
}
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? - 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]
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'"? - 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;
-
}
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!
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 - 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: - // 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;
-
}
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: -
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.
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: -
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.
Great, I'm glad that works for you!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
by: Yueh-Wei Hu |
last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news:
==============================================================
> > Question 1:
> >...
|
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':...
|
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
|
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...
|
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....
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |