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

    [RESOLVED] Cannot convert parameter

    Hey.

    I can't seem to understand why the compiler is giving me this error:
    Code:
    error C2664: 'bool Collision::is_3d_collision(const Entity &,const Entity &)' : cannot convert parameter 2 from 'NPC' to 'const Entity &' traffic_manager.h	48
    Code:
    typedef boost::shared_ptr<Car> car_ptr;
        typedef std::vector<car_ptr> car_vec;
        typedef car_vec::iterator car_it;
        typedef car_vec::const_iterator const_car_it;
        car_vec cars;
    
        class Car_Coll_Notify_Pred
        {
        public:
            Car_Coll_Notify_Pred(npc_vec& NPCs)
            : NPCs(NPCs)
            {
            }
            
            void operator()(car_ptr car)
            {
                for(npc_it it = NPCs.begin(); it != NPCs.end(); ++it)
                {
                    if(Collision::is_3d_collision(*car, **it))
                    {
                        car->On_Collision(**it);
                    }
                }
            }
        private:
            npc_vec& NPCs;
        };
    After de-referencing the iterator and shared_ptr, the call to is_3d_collision should be passing an NPC object (which is derived from Entity), but the compiler says it can't convert a NPC to a const Entity& which I don't understand, since polymorphism should have come into play here.

    npc_ptr etc. typedefs are declared as:
    Code:
    class NPC;
    
    typedef boost::shared_ptr<NPC> npc_ptr;
    typedef std::vector<npc_ptr> npc_vec;
    typedef npc_vec::iterator npc_it;
    typedef npc_vec::const_iterator const_npc_it;
    Code:
    inline bool is_3d_collision(const Entity& e1, const Entity& e2)
        {
            return is_3d_collision<Entity::vector3_t>(e1, e2.Get_Pos(), e2.Get_Bounds());
        }
    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?

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

    Re: Cannot convert parameter

    Hi Mybowlcut,

    the only thing that comes to my mind is that you could have forgotten the <public> keyword in the NPC class declaration when inheriting from Entity. (... it sometimes happens to me when I'm low on coffee ).

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

    Re: Cannot convert parameter

    Hey.

    NPC actually derives from Character which then derives from Entity.. sorry, should have mentioned that. That shouldn't change anything though, as it's still an Entity derivee. The public keyword is there in all the classes as I've only used public inheritance for this.

    For now I have casted the argument to an Entity&, but I'm still interested to know why this is happening.

    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?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cannot convert parameter

    Quote Originally Posted by Mybowlcut View Post
    Hey.

    I can't seem to understand why the compiler is giving me this error:
    Can you come with a simple, standard, C++ example using simple, do nothing classes that reproduces the error? Not only will it be easier to solve your problem directly, maybe you will see what's wrong if you come up with a simple example.

    Second, double indirection (more than one "*"), is more than likely the problem, since multiple indirections do not go on the same rules as single indirection.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 25th, 2009 at 06:31 AM.

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

    Re: Cannot convert parameter

    This compiles fine.. but I can't see the difference between it and my example above:

    Code:
    #include "boost/shared_ptr.hpp"
    
    #include <vector>
    
    class Entity
    {
    };
    
    class Character : public Entity
    {
    };
    
    class NPC : public Character
    {
    };
    
    class Car : public Entity
    {
    };
    
    typedef boost::shared_ptr<NPC> npc_ptr;
    typedef std::vector<npc_ptr> npc_vec;
    typedef npc_vec::iterator npc_it;
    
    typedef boost::shared_ptr<Car> car_ptr;
    typedef std::vector<car_ptr> car_vec;
    typedef car_vec::iterator car_it;
    
    void func(const Entity& e1, const Entity& e2)
    {
    }
    
    int main()
    {
        npc_vec NPCs;
        car_ptr car(new Car());
    
        for(npc_it it = NPCs.begin(); it != NPCs.end(); ++it)
        {
            func(*car, **it);
        }
    }
    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?

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Cannot convert parameter

    Quote Originally Posted by Mybowlcut View Post
    Hey.

    I can't seem to understand why the compiler is giving me this error:
    Code:
    error C2664: 'bool Collision::is_3d_collision(const Entity &,const Entity &)' : cannot convert parameter 2 from 'NPC' to 'const Entity &' traffic_manager.h	48
    Did you use a forward declaration for class NPC? If so, the compiler doesn't know it inherits from Entity. You will need to include the class definition before you use an instance of it.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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

    Re: Cannot convert parameter

    Geez... funny how you can forget such simple things after looking at something for too long.

    Cheers D_Drmmr!
    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