Using one one class' integer in a other class

I am trying to use one Class in a other one but it shows me an error code:
E0077


First Class:
1
2
3
4
5
6
7
8
class PlayerInv
{
public:
	PlayerInv();
	~PlayerInv();
	int ItemsID[10];

};


Second Class:
1
2
3
4
5
6
7
8
9
class Player {

private:
	PlayerInv PLInv;
	PLInv.ItemsID[1] = 2; // Error is right here
public:
	int hp;
	float speed;
};
Last edited on
you can't do that, you can initialize it in the constructor for the class (which isnt there yet, you need to add it). Classes don't like initialize in the body, that is the issue.
Last edited on
C++11 did add brace initialization: http://www.informit.com/articles/article.aspx?p=1852519

Therefore, one could be able cope with default constructors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

struct Foo {
  int x[2];
};

struct Bar {
  Foo y {{7, 42}};
};

int main()
{
  Bar z;
  std::cout << z.y.x[1] << '\n';
}

However, writing explicit constructors is still a valid idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

struct Foo {
  int x[2];
  Foo( int a, int b ) : x{a,b} {}
};

struct Bar {
  Foo y;
  Bar() : y(7,42) {}
};

int main()
{
  Bar z;
  std::cout << z.y.x[1] << '\n';
}
Topic archived. No new replies allowed.