Thread: Initializing a class reference inside another class...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Initializing a class reference inside another class...

    I have the following two classes.
    Code:
    class Surface;
    class Sprite;
    Surface is just a basic wrapper for some of the SDL functions, and holds clipping information, which is unimportant at the moment.

    Sprite contains a reference to Surface.
    Code:
    class Sprite
    {
    private:
       Surface& sprite;
       //other members here
    public:
      //functions and stuff here
    };
    Naturally it will not allow me to compile this because it has to initialize sprite(the Surface&).
    Code:
    1>------ Build started: Project: Project-Varia, Configuration: Debug Win32 ------
    1>Compiling...
    1>Sprite.cpp
    1>c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'Surface &'
    1>c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.cpp(6) : error C2439: 'Sprite::sprite' : member could not be initialized
    1>        c:\documents and settings\raigne\my documents\visual studio 2008\projects\project-varia\project-varia\sprite.h(6) : see declaration of 'Sprite::sprite'
    1>Build log was saved at "file://c:\Documents and Settings\Raigne\My Documents\Visual Studio 2008\Projects\Project-Varia\Project-Varia\Debug\BuildLog.htm"
    1>Project-Varia - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    It says that it must be done in the initializer list, but I cant figure it out. The full Sprite, and Surface classes are below. I greatly appreciate any insight as to the solution to my predicament.
    Code:
    #pragma once
    
    class Surface
    {
    private:
        SDL_Surface* surface;
        std::vector<SDL_Rect> clips;
    
        bool usable_;
        bool debug_;
    public:
        //Constructor
        Surface();
        Surface(const std::string& imagefile);
    
        //Destructor
        ~Surface();
        
        //Debug
        void SetDebug(bool d) { debug_ = d; };
    
        //Surface usability function
        const bool& GetUsable() const { return usable_; };
    
        //Compare rects function
        bool ComRects(const SDL_Rect& a, const SDL_Rect& b);
    
        //Access the surface
        SDL_Surface* GetSurface();
    
        //Access rect by ID
        SDL_Rect&    GetClip(const unsigned int& rectID) { if ( rectID <= clips.size() ) { return clips.at(rectID); } };
    
        //Load a new surface
        bool LoadSurface(const std::string& imagefile);
    
        //Free surface
        void FreeSurface() { SDL_FreeSurface(surface); };
    
        //Create a new clip
        void CreateClip(const int& x, const int& y, const int& w, const int& h);
    
        //Delete a clip
        void DeleteClip(const int& clipID);
    };
    Code:
    #pragma once
    
    class Sprite
    {
    private:
        Surface& sprite;
        SDL_Rect position;
    
        std::vector<std::vector<int> > clipbook;
        struct
        {
            short cur_page;
            short cur_clip;
            short start_clip;
            short last_clip;
        };
    public:
        Sprite();
        ~Sprite();
    
        void SetSprite(Surface& surface);
        Surface& GetSprite() { return sprite; };
        
        //clipbook functions
        void AddPage(); //adds a page to the back of the book
        void AddClip(const int& page, const int& clip);//adds a clip to the end of a page
        
        void WarpSprite(const int& x, const int& y);
        void MoveSprite(const int& x_axis, const int& y_axis);
    
        void AnimateClipPage(const int& page, const int& start_clip = 0);
    };
    Last edited by Raigne; 02-03-2008 at 11:50 PM. Reason: Typo, and the actual error message

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must initialize references in the initializing section of the constructor:
    Code:
    class foo
    {
    private:
    	int& m_n;
    public:
    	foo(int& n) : m_n(n) { }
    };
    
    int main()
    {
    	int n = 0;
    	foo a(n);
    	return 0;
    }
    In other words, you can't construct an object and later bind the reference. That's a no-no.
    If that's the case, you need to use pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Can i change the reference through a function at a later time? say if i wanted to use a different surface on the same sprite.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope.
    References are bound as they are created and remains bound to the same object.
    Anything you try to do, including reassigning, will only happen at the object pointed to.
    For this, you must use pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. problem with const int inside class (c2258)
    By talz13 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 07:34 PM