Why can I declare a float like:
Player.h (compiles)
#include "Component.h"
#include "Vector2.h"
class Player : public Component
{
public:
float positionX;
float positionY;
};
but can't declare a my Vector2 struct like:
Player.h (does not compile)
#pragma once
#include "Component.h"
#include "Vector2.h"
class Player : public Component
{
public:
Vector2 position;
};
Vector2.h
#pragma once
struct Vector2
{
Vector2(float t_x, float t_y);
float x;
float y;
};
Vector2.cpp
#include "Vector2.h"
Vector2::Vector2(float t_x, float t_y)
{
x = t_x;
y = t_y;
}
I'm new to C so I might be doing something completely wrong, but I don't know what. I just want to declare a variable of type Vector2 to use inside of my Player.cpp file.
CodePudding user response:
If your Vector2 doesn't have a parameter-less constructor, C won't know how to construct it.
You need to declare how to construct your Vector2 in the Player constructor using parameter initialization, like so:
Player::Player() : position(0,0) {
// player initialization here
}
Another option is to store an std::unique_ptr to your Vector2, leaving the initialization of the Vector2 at runtime.