Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: General C++ question: Defining a struct within a class  (Read 19050 times)

0 Members and 1 Guest are viewing this topic.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
General C++ question: Defining a struct within a class
« on: July 11, 2013, 09:00:54 am »
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.

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #1 on: July 11, 2013, 09:30:54 am »
Is your struct definition declared public in your headerfile?

class Camera
{
//...
public:
  struct CameraProperties
  {
      int somethingsomething;
      float thisandthat;
  };
//...
};
 

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #2 on: July 11, 2013, 09:52:12 am »

I think I figured out how to define it:

I have it defined as private in the Camera headerfile like so:
Code: [Select]
struct m_CameraProperties;
Then in the .cpp file I can define it like so:
Code: [Select]
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...


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #3 on: July 11, 2013, 10:16:11 am »
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.
Laurent Gomila - SFML developer

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #4 on: July 11, 2013, 10:16:41 am »
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?
« Last Edit: July 11, 2013, 10:18:28 am by cpolymeris »

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #5 on: July 11, 2013, 10:43:01 am »
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.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #6 on: July 11, 2013, 10:49:50 am »
Hah, you're right...actually it works if I move the function below the private: declarations...unbelievable.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: General C++ question: Defining a struct within a class
« Reply #7 on: July 11, 2013, 03:53:06 pm »
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)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor