-2

I'm trying to pass an object and a vector of class objects or two objects to two overloaded template functions named checkPosition.
No matter what I try, changing my function to passing by value / reference / pointer / const / not const, it gives me the error;
Error: no instance of overloaded function "Room::checkPosition" matches the argument list
argument types are: (const Player, const std::vector< Monster, std::allocator< Monster > > )
object type is: const Room.

From Room.h:

class Room
{
    public:
        Player player;
        std::vector<Monster> monster;
        std::vector<Wall> wall;
        std::vector<Exit> exit;

        template<class T1> void xSet( T1 &object, const int &input );
        template<class T2> void ySet( T2 &object, const int &input );
        template<class T3> int x( T3 &object );
        template<class T4> int y( T4 &object );
        template<class T5> bool checkPosition( const T5 &object1, const T5 &object2 );
        template<class T6> bool checkPosition( const T6 &object1, const std::vector<T6> &object2 );
        void objectIconSet( );

        void lengthSet( const int &input );
        void widthSet( const int &input );
        int length( );
        int width( );
        void staticDataMap( );
        void completeDataMap( );
        char staticDataMap( int x, int y );
        char completeDataMap( int x, int y );

    private:
        int _length;
        int _width;
        std::vector< std::vector<char> > _staticDataMap;
        std::vector< std::vector<char> > _completeDataMap;
};

From Room.cpp

template<class T5> bool Room::checkPosition( const T5 &object1, const T5 &object2 )
{
    if( object1.position.x == object2.position.x &&
        object1.position.y == object2.position.y )
    {
        return true
    }

    return false;
}
template<class T6> bool Room::checkPosition( const T6 &object1, const std::vector<T6> &object2 )
{
    for( int i = 0; i < object2.size( ); i++ )
    {
        if( object1.position.x == object2[i].position.x &&
            object1.position.y == object2[i].position.y )
        {
            return true
        }
    }

    return false;
}

Examples of function use in main.cpp:

bool checkWinCondition( const Room &room )
{
    if( room.checkPosition( room.player, room.exit ) == true )
    {
        std::cout << "\nYou win!";
        return true;
    }

    return false;
}
bool checkLoseCondition( const Room &room )
{
    if( room.checkPosition( room.player, room.monster ) == true )
    {
        std::cout << "\nYou lose!";
        return true;
    }

    return false;
}

void SetRoomOuterWalls( Room &room )
{
    Position tempPosition;
    Wall tempWall;

    for( int y = 0; y < room.length( ); y++ )
    {
        for( int x = 0; x < room.width( ); x++ )
        {
            tempPosition.x = x;
            tempPosition.y = y;
            if( room.checkPosition( tempPosition, room.exit ) == true )
            {
                continue;
            }
            else if( x == 0 || x == room.width( ) - 1 ||
                     y == 0 || y == room.length( ) - 1 )
            {
                room.wall.push_back( tempWall );
                room.xSet( room.wall, x );
                room.xSet( room.wall, y );
            }
        }
    }
}
Snizzlenose
  • 1
  • 1
  • 2
  • Just for the record: The next problem you'll going to hit [will be solved here](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – πάντα ῥεῖ Apr 27 '15 at 18:49
  • You're calling a non-`const` function on a `const` object. Also, you don't have to use a different template typename in every function (`T1`, `T2`, ...). You can just use `T`. – Barry Apr 27 '15 at 18:49
  • You said the type in the vector is the same as the type of the first argument. Of course a `Player` as the first argument and `Monster` as the type in the vector isn't going to work. They're not the same like you've said they have to be. – chris Apr 27 '15 at 18:51

1 Answers1

0

You are calling your function with two different parameters but your function is templated to one type

template<class T6> bool checkPosition( const T6 &object1, const std::vector<T6> &object2 );

Means that that you want an object and a vector of objects of the same type. You are passing into the function a player and a vector<monster> which does not match. What you can do is change your template to:

template<class T, class Y> bool checkPosition( const T &object1, const std::vector<Y> &object2 );

Which will allow you to take some object and a vector of the same type or another type.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • That fixed it, as well as removing const as an argument type. I was under the impression that a template type could be many types and only one was needed for what I was doing, I guess I was wrong. – Snizzlenose Apr 27 '15 at 19:06