CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Cannot convert from 'X' to 'Y'

    Hey.

    I'm running into a problem with a class that uses template metaprogramming. I'm not well-versed with the methodology, so I'm a bit confused as to why I'm getting the error.

    The screen object I'm trying to create:
    Code:
    #define GAME_SCREEN_TYPES TL6(Image, Button, Slider, \
    Dialog_Box, Progress_Bar, Input_Box)
    
    class Game
    {
        // ...
    private:
        boost::shared_ptr<Screen<GAME_SCREEN_TYPES> > game_screen;
    };
    Creating the screen object:
    Code:
    Game::Game()
    : game_screen(Screen<GAME_SCREEN_TYPES>::Create(STGS.GUI_Dir() / "_game_screen.xml"))
    {
    }
    The error(s):
    Error 1 error C2440: 'initializing' : cannot convert from 'Type_List<T,U> *' to 'Screen<Type_List> *' e:\dev\boost\boost\smart_ptr\shared_ptr.hpp 179
    Error 2 error C2439: 'boost::shared_ptr<T>:x' : member could not be initialized e:\dev\boost\boost\smart_ptr\shared_ptr.hpp 179
    The screen class:
    Code:
    // Type used to mark the end of a typelist.
    struct Null_Type
    {
    };
    
    // The typelist. For nested typelists type U is a typelist again, so one can
    // build typelists of any length.
    template<typename T, typename U>
    struct Type_List
    {
       typedef T head; 
       typedef U tail; 
    };
    
    // Convenience macros for building typelists.
    #define TL1(T1)									    Type_List<T1, Null_Type >
    #define TL2(T1,T2)								    Type_List<T1, TL1(T2) >
    // ...
    
    template<typename Type_List>
    class Screen : public Base_Screen
    {
    public:
        static boost::shared_ptr<Screen<Type_List> > Create()
        {
            return boost::shared_ptr<Screen<Type_List> >(new Type_List());
        }
    
        static boost::shared_ptr<Screen<Type_List> > Create(const boost_path& file_name)
        {
            boost::shared_ptr<Screen<Type_List> > ptr(Create());
            ptr->Read(file_name);
            return ptr;
        }
    
    	virtual ~Screen()
    	{
    		LOG("Screen at " + file_name.file_string() + " cleaned up.");
    	}
    protected:
        Screen() {}
    
        Screen(const boost_path& file_name)
    	: file_name(file_name)
    	{
    		Read(file_name);
    		LOG("Screen at " + file_name.file_string() + " initialised.");
    	}
    
        // ...
    };
    Base_Screen:
    Code:
    class Base_Screen
    : public GUI_Object
    , public ticpp::File_Readable
    {
    public:
    	virtual ~Base_Screen() {}
    };
    Cheers.
    Last edited by Mybowlcut; January 15th, 2011 at 12:58 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Cannot convert from 'X' to 'Y'

    the error has not to do with metaprogramming; it's simply saying that you cannot construct a Screen<Type_List> shared pointer from a Type_List pointer:

    Code:
    return boost::shared_ptr<Screen<Type_List> >(new Type_List());
    should be

    Code:
    return boost::shared_ptr<Screen<Type_List> >(new Screen<Type_List>());
    BTW, using the same name for the Type_List<> template and for the Screen Type_List template parameter it's probably not a good idea ...
    Last edited by superbonzo; January 15th, 2011 at 04:04 AM.

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Cannot convert from 'X' to 'Y'

    Quote Originally Posted by superbonzo View Post
    the error has not to do with metaprogramming; it's simply saying that you cannot construct a Screen<Type_List> shared pointer from a Type_List pointer:

    Code:
    return boost::shared_ptr<Screen<Type_List> >(new Type_List());
    should be

    Code:
    return boost::shared_ptr<Screen<Type_List> >(new Screen<Type_List>());
    BTW, using the same name for the Type_List<> template and for the Screen Type_List template parameter it's probably not a good idea ...
    I only mentioned it because I couldn't find the source of the error, so thought it may be due to the metaprogramming code. I see now that I am completely blind!

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured