c++ player me / me.name

Hello. I am learning c++ from the "learning c++ by creating games in UE4" book and there is a section i am stuck on. I have copied the code from the book, it is the exact same but there is problems in it, specifically with the Player me and the me.xxxxx sections, they do not work. Can someone tell me if the book is wrong or what if they know thanks. Also like i said I have just started learning so if possible can you use dumb language for me XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <string>

using namespace std;


struct Player
{
	string name;
	float hp;
	vector position;
};

int main()
{
	player me;
	me.name = "William";
	me.hp = 100.0f
	me.position.x = me.position.y = me.position.z = 0;

}
Can you elaborate on what you mean by it "not working?"

The first thing that I've noticed is that this "vector" type isn't actually defined anywhere. Also, int main() needs to return a value. Try writing return 0; right before the function's closing bracket.
Also, int main() needs to return a value.
main returns the value 0 if no return statement is encountered before the end of the function. Explicitly providing one is redundant. (This is only true of main.)
Well I have just got the player part to work (which i have been stuck with for over a month, im such an idiot XD) but the vector part. Where would i place the struct vector, i know if i put it under the struct player it does not work. Invalid combination of type specifiers it says.

edit

I have tried it this way aswel but it still does not work, the error is incomplete type is not allowed

#include <iostream>
#include <string>

using namespace std;


struct Player

{
string name;
float hp;
};

struct vector

{
vector position;
};

int main()
{
Player me;
me.name = "William";
me.hp = 100.0f;
me.position.x = me.position.y = me.position.z = 0;

}
Last edited on
This sticks out like a sore thumb:

1
2
3
4
struct vector
{
	vector position;
};


What exactly do you expect this to do?

Your vector should have x, y, (z) members. Your Player should have a position vector.
I am just writing out the books examples, just to see how things work. But it did not teach how to do struct player and struct vector in the same code, so i just placed them together and i get the error incomplete type is not allowed. like i said, this is all brand new to me, ive never done coding before.
I'm not familiar with the book, but I can understand your frustration.

I would recommend not calling your position vector. I changed it to vector3, as it a vector in three dimensional space. There is a vector in c++, but that is something completely different from what you want.

Here's code to do what you want. Ask questions if you don't understand.

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
#include <iostream>
#include <string>

using namespace std;

struct vector3
{
    float x;
    float y;
    float z;
};

struct Player
{
	string name;
	float hp;
	vector3 position;
};

int main()
{
	Player me;
	me.name = "William";
	me.hp = 100.0f;
	me.position.x = me.position.y = me.position.z = 0;
    std::cout << me.position.x << ", " << me.position.y << ", " << me.position.z << std::endl;
}
Thank you mgoetschius, that code worked perfectly.
Hey, I read that book too, It worked, you didn't include the previous code snippet for the vector
ah thanks RUNNER PRO AGARIO (84), think it was how they had it written that confused me on how to include the two structs.

Runner pro agario, after you finished that book, did you start to learn from a new one or did it educate you enough to write code for games?
Me, no I got another book after, also still on UE4, but it taught me. I personally didn't like the book though, since it was unclear at many times.
Topic archived. No new replies allowed.