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

Author Topic: Noob question about classes  (Read 2283 times)

0 Members and 1 Guest are viewing this topic.

sgthale

  • Newbie
  • *
  • Posts: 9
    • View Profile
Noob question about classes
« on: December 09, 2010, 06:37:34 am »
Im very new to C++ myself and i have a quick class question. So i have something like this:

Code: [Select]
class Frame{
    public:
        Frame( const sf::Image& img, const sf::IntRect& rec);
        sf::Image& frame_img;
        sf::IntRect frame_rect;
};
Frame::Frame( const sf::Image& img, const sf::IntRect& rec){
    frame_img = img&;
    frame_rect = rec;
}


But i dont know how to make it so the bottom constructor sets the values for the class´s frame_img and frame_rect

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Noob question about classes
« Reply #1 on: December 09, 2010, 11:33:08 am »
You get a compiler error complaining about the reference (like «Error: uninitialized reference member 'Class::variable'»), right ?

First, img& doesn't mean anything.

A reference must be bound to anther variable when it's constructed.

For example :
Code: [Select]
void f(void) {
  int i = 42;

  int& r; // Error: 'r' declared as reference but not initialized

  int& r2 = i; // Correct
}


When you are in a class, you must use the initialization-list to bound your reference to something. Like this :

Code: [Select]
class C {
  int& r;
public:
  C(int& r_) : r(r_) { }
};


The behaviour is the same with sf::Image.
SFML / OS X developer

sgthale

  • Newbie
  • *
  • Posts: 9
    • View Profile
Noob question about classes
« Reply #2 on: December 10, 2010, 04:09:53 am »
yeah im still confused on how to apply that onto my code. what does "r_" mean?

sgthale

  • Newbie
  • *
  • Posts: 9
    • View Profile
Noob question about classes
« Reply #3 on: December 10, 2010, 05:16:16 am »
i managed to get the sf::IntRect to work but not the sf::Image


Code: [Select]
class Frame{
    public:
        Frame(const sf::Image& image, const sf::IntRect& rect);
        sf::Image& frame_image;
        sf::IntRect frame_rect;
};
Frame::Frame(const sf::Image& image, const sf::IntRect& rect)
:    frame_image(&image)
,    frame_rect(rect)
{
}


I keep getting this error:

Invalid initialization of non-const reference of type 'sf::Image&' from a temporary of type 'const sf::Image*'|



dont know how to fix that

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Noob question about classes
« Reply #4 on: December 10, 2010, 08:45:47 am »
«r_» is only a name, it could have been «fenouil» – it's only a sequence of letter.

Now about your compiler error, I read «frame_image(&image)». It means that you take the address of «image» with '&' in front of it.

Tip : When we work with references, we never care about addresses.

Now let's study what says the compiler : Invalid initialization of non-const reference of type 'sf::Image&' from a temporary of type 'const sf::Image*'|

He tells us that you try to initialize your reference with a pointer of type 'const sf::Image*'. With above tip you should be able to spot the error in your code.  :wink:

Unfortunately it's not the only error so you will get another error message when you have corrected this one.

You'll see something about «const». The problem is that you give to your constructor a «const sf::Image&» variable and you store it (into your class field) as a «sf::Image&». You can see that the «const» is lost thus the compiler doesn't allow you to proceed that way.
SFML / OS X developer