![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
Function array
I've hit a brick wall when trying to get a function to return an int array. Here's what my code looks like:
//prototype
int GetInvent();
//definition
int Player::GetInvent()
{
for(int i = 0; i < inventory; i++)
{
return inventorymax[i];
}
}//prototype
int[] GetInvent();
//definition
int[] Player::GetInvent()
{
for(int i = 0; i < inventory; i++)
{
return inventorymax[i];
}
}
// And
//prototype
int GetInvent()[];
//definition
int Player::GetInvent() []
{
for(int i = 0; i < inventory; i++)
{
return inventorymax[i];
}
}I didn't think my second solution would work, but niether of them did so, any help?
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
I see a couple problems with this (first solution).. first of all are you trying to return the whole array, or are you trying to return a position in your array? if your trying to return the whole array, you need to have the return time of vector<int>, instead if just int. and another thing that seemed a bit off was your for loop; you have it returning a position in the array... but it would only loop through once and hit the return an exit this fucntion. try this if your trying to return the whole array... i am assuming your array is a vector, if not you will get the general idea...
//definition
std::vector<int> Player::GetInvent()
{
return inventorymax;
}
__________________
I am Addicted to Linux! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
I really don't understand what you want to do the way you put it. Maybe you will need to provide some more code, so that we can see what is 'inventorymax'.
But basically, regardless wether returning a whole array is possible, you can set your functions as 'void' and modify the functions so that they can work with pointers. That way, when you call the functions, the arrays that you have given as arguments will be changed. No need to return anything that way. EDIT: I think that I have found something that may help you: check this out. Seems you cannot return a whole array. Look at this in the page I gave you: #include <iostream>
#define LENGTH 5
using std::cout;
using std::endl;
using std::cin;
int* getArray();
void printArray(int* array);
int main(){
int *p;
cout << p;
return 0;
}
int* getArray()
{
int *array = new int[LENGTH];
for ( int i = 0; i < LENGTH; i++ )
{
cout << "Enter an integer: ";
cin >> array[i];
}
cout << "array in getArray()" << endl;
printArray(array);
return array;
}
void printArray(int* array)
{
for ( int i = 0; i < LENGTH; i++ )
cout << *(array+i) << " ";
cout << endl;
cout << endl;
}
__________________
Project::Soulstorm (personal homepage) Last edited by Soulstorm; Mar 30th, 2006 at 4:00 AM. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Within a class it is something like:
#include <iostream>
using namespace std;
class Player
{
private:
int inventorymax[10];
int inventory;
public:
Player();
void SetInventory(int inv);
int* GetInvent();
int GetInventItem();
};
Player::Player()
{
inventorymax[0] = 3;
inventorymax[1] = 2;
inventorymax[2] = 1;
}
void Player::SetInventory(int inv)
{
inventory = inv;
}
int* Player::GetInvent()
{
return inventorymax;
}
int Player::GetInventItem()
{
static int i = 0;
//for(; i < inventory;)
while(i < inventory)
{
return inventorymax[i++];
}
return 0;
}
int main()
{
int inv = 3;
Player player1;
player1.SetInventory(inv);
int *array = player1.GetInvent();
cout << array[0] << array[1] << array[2] << endl;
for(int i = 0; i < inv; i++)
cout << player1.GetInventItem() << endl;
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates Last edited by nnxion; Mar 30th, 2006 at 4:41 AM. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
The closest things you can get to returning an array follow. I assume for sake of discussion you want to return an array of int.
1) Return a pointer to a statically allocated array; int *Array()
{
static int array[5];
return array;
}2) Return a pointer to a dynamically allocated array; int *Array(int length)
{
return new int[length];
}3) Return a structure that contains an array; typedef struct {int x[5];} IntArray;
IntArray Array()
{
IntArray x;
return x;
}4) Use a C++ class that encapsulates an array, and provides necessary operators (eg operator[]) to allow direct access of elements. This is an extended form of one of the previous methods. For example; class IntArray
{
public:
IntArray(int size) : array(new int[thesize]), size(thesize) {};
IntArray(const IntArray &x) : array(new int [x.size]), size(x.size) {};
IntArray &operator=(const IntArray &x)
{
IntArray temp(x);
int *itemp = temp.array;
temp.array = array;
array = itemp;
int isize = temp.size;
temp.size = size;
size = isize;
return *this;
};
~IntArray() {delete [] array;};
int &operator[](int index)
{
if (index < 0 && index >= size) ComplainBitterly();
return array[index];
};
int operator[](int index) const
{
if (index < 0 && index >= size) ComplainBitterly();
return array[index];
};
private:
int *array;
int size;
};
IntArray Array(int size)
{
IntArray x(size);
return x;
}5) Return a std::vector<int>. This is a special case of the previous method, except that you don't have to roll your own. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2006
Location: Macedonia
Posts: 12
Rep Power: 0
![]() |
You can't return a whole array think for a bit how arrays go in memory
Memory location Value 1 32 2 34 3 36 4 38 5 39 int a[10]; SomeFunction(a); |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|