-
Hi all, this is a general question about C++. I'm trying to make a Camera class which will contain 2 private member values: an sf::View and a struct containing various camera properties. So basically my idea is, outside this class, you will be able to call Camera.setProperties(<STRUCT CONTAINING DESIRED PROPERTIES>) and that will effectively set up the entire Camera class. So in Camera.h I have my private struct m_CameraProperties. In Camera.cpp I have the definition of that struct - struct Camera::m_CameraProperties{<stuff>};
But when I try to declare my setProperties(<camera properties struct>) it's not letting me use the struct in here. Any ideas? I know I could just ditch a struct and use several variables but I'd like to be able to pass a neat little structure instead of a bunch of random variables. Thanks for any help.
-
Is your struct definition declared public in your headerfile?
class Camera
{
//...
public:
struct CameraProperties
{
int somethingsomething;
float thisandthat;
};
//...
};
-
I think I figured out how to define it:
I have it defined as private in the Camera headerfile like so:
struct m_CameraProperties;
Then in the .cpp file I can define it like so:
struct Camera::m_CameraProperties{
bool IsYLocked;
bool IsXLocked;
float YMinimum;
float XMinimum;
float YMaximum;
float YMinimum;
};
Still trying to figure out how to declare it as a parameter in a Camera member function...
-
What is the error message?
Keep in mind that:
- you must declare the structure before the function that uses it
- if the function takes the struct argument by copy, the full definition must be in the header, not only the forward declaration (struct m_CameraProperties)
By the way, m_CameraProperties is a strange name for a type; usually the m_ prefix is for member variables.
-
If you want to use it as a parameter, the definition must be public (in the headerfile), the declaration can be private (in the headerfile, too, but instantiated in the cpp if it is static).
//Camera.hh
class Camera
{
public:
struct CameraProperties
{
//...
};
private:
CameraProperties properties;
public:
void someMethod(const CameraProperties &);
};
How else are the consumers of the class going to know what to pass to your method?
-
I would think there's a way to keep it a private member. I'm getting confused now as to what's the type definition and where's the variable definition...I will read up on the basics of defining structs I guess. They have always confused me as I've seen them declared with typedef and sometimes they are not. Now with the extra layer of being within a class I'm extra confused.
Anyways I think I'm getting closer:
Camera.h
class Camera
{
public:
Camera();
~Camera();
void setProperties(Camera::TCameraProperties* cameraProperties); // Still complains saying Camera has no member TCameraProperties
private:
struct TCameraProperties;
TCameraProperties* m_CameraProperties;
sf::View m_View;
};
Moving the struct defs to public doesn't make a difference.
-
Hah, you're right...actually it works if I move the function below the private: declarations...unbelievable.
-
Hah, you're right...actually it works if I move the function below the private: declarations...unbelievable.
To quote Laurent...
Keep in mind that:
- you must declare the structure before the function that uses it
- if the function takes the struct argument by copy, the full definition must be in the header, not only the forward declaration (struct m_CameraProperties)