Initializing the contents of an aray to those of another

closed account (DL30RXSz)
Basically, in the constructor, I want to pass through the characters of one array into those of another. I'm obviously not doing it properly since I get this error:
(22) : error C2440: '=' : cannot convert from 'char []' to 'char [10]'
(23) : error C2440: '=' : cannot convert from 'int []' to 'int [1][1]'
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

class Player
{
public:
	Player(bool tempLife, char tempName[10],
		   int tempLocation[1], int tempId);
	bool life;
	char name[10];
	int location[1][1];
	int id;
private:
	bool GetLife(){return life;}
	void SetLife(bool temp){life = temp;}
};

Player::Player(bool tempLife, char tempName[10],
		   int tempLocation[1], int tempId)
	   {
		   life=tempLife;
		   name=tempName;
		   location=tempLocation;
		   id=tempId;
	   }

int main()
{
	return 0;
}
memcpy(name,tempName,10);

What's the point of making location an array of 1 by 1? That's just a single int. The same goes to tempLocation.
closed account (DL30RXSz)
I got confused with arrays. I really wanted [2][1]. What do I do for size in a multidimensional array for memcopy?
All elements are stored adjacently. Simply multiply the sizes of their dimensions.
closed account (DL30RXSz)
Okay, another problem. I'm having trouble passing the array data through the function. When I try to run, I get a runtime error saying mainTempLocation is corrupt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class Player
{
public:
	Player(bool tempLife, char tempName[10],
		   int tempLocation[2][1], int tempId);
	bool life;
	char name[10];
	int location[2][1];
	int id;
private:
	bool GetLife(){return life;}
	void SetLife(bool temp){life = temp;}
};

Player::Player(bool tempLife, char tempName[10],
		   int tempLocation[2][1], int tempId)
	   {
		   life=tempLife;
		   memcpy(location, tempLocation, 2);
		   memcpy(name, tempName, 10);
		   id=tempId;
	   }

int main()
{
	int mainTempLocation[2][1];
	mainTempLocation[0][1]=5;
	mainTempLocation[1][1]=3;
	Player Players(1, "Colter", mainTempLocation, 0);
	return 0;
}
[2][1]
For some reason, I didn't notice this before. It makes no sense to have an array with a dimension of 1. [2][1] and [2] do exactly the same thing.

mainTempLocation[0][1]=5;
mainTempLocation[1][1]=3;
You're writing outside the array. The second dimension is only 1 element long, which means its last element is zero.

It's easier to just make it a one-dimensional array.
closed account (DL30RXSz)
I'm forgetting how to use arrays properly today :S. I changed name into a string to save some trouble.

My new problem is that I need a way to return an array in an accessor function to view its contents. I looked it up and the only method I could find was returning a pointer to the array. This, for me, would defeat the purpose of using protected memberes, and probably wouldn't work anyway. What's the best way of creating an accessor for an array?

I found a couple other bugs in my code and made the changes you suggested. Here is the updated code if you need it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

class Player
{
private:
	bool life;
	string name;
	int location[2];
	int id;
public:
	Player(bool tempLife, string tempName,
		   int tempLocation[2], int tempId);
	bool GetLife(){return life;}
	string GetName(){return name;}
	void SetLife(bool temp){life = temp;}
        //need an accessor for location here.
};

Player::Player(bool tempLife, string tempName,
		   int tempLocation[2], int tempId)
	   {
		   life=tempLife;
		   memcpy(location, tempLocation, 2);
		   name=tempName;
		   id=tempId;
	   }

int main()
{
	int mainTempLocation[2];
	mainTempLocation[0]=5;
	mainTempLocation[1]=3;
	Player Player0(1, "Colter", mainTempLocation, 0);
	cout << Player0.GetLife() << endl;
	cout << Player0.GetName() << endl;

	int sleep;
	cin >> sleep;
	return 0;
}
You could either return it as a pointer to const data, or instead of returning it, copy it to a passed pointer.

Oh, and your memcpy() on line 25 is wrong. The third parameter should be 2*sizeof(int). I didn't mention that for the char array because sizeof(char)==1 is always true.
closed account (DL30RXSz)
When I try to do this I have to create a local array for each function that needs to access the location array. Is this considered good programming practice? Also I'm not sure how to do it properly. Here's how I have it so far.
Player location accessor.
int * GetLocation(){return location;}
In main:
1
2
int * player0Location[2];
	player0Location=Player0.GetLocation();


This is the only way I can think of to do this but of course it gives me an error.
'=' : cannot convert from 'int *' to 'int *[2]'
Your function returns a pointer, not an array of pointers.

This would work:

1
2
3
4
5
int* player0Location = Player0.getLocation();

// change the location:
player0Location[0] = 1;
player0Location[1] = 2;


As to whether or not that's good practice, it doesn't strike me a "great", but I've seen (and written) worse.
Topic archived. No new replies allowed.