传递单个或向量对象时没有重载函数的实例

No instance of overloaded function when passing single or vector of objects

本文关键字:重载 函数 实例 单个 向量 对象      更新时间:2023-10-16

我正在尝试将一个对象和类对象的向量或两个对象传递给两个名为 checkCurrent 的重载模板函数。
无论我尝试什么,将我的函数更改为按值/引用/指针/常量/非常量传递,它都会给我错误;
错误:重载函数"Room::checkPosition"的实例与参数列表
不匹配参数类型为:(const Player, const st::vector<Monster,>> )
对象类型为:常量房间。

从房间.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;
};

从房间.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;
}

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 );
            }
        }
    }
}

您正在使用两个不同的参数调用函数,但您的函数被模板化为一种类型

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

表示您需要一个对象和一个相同类型的对象的向量。 您正在向函数传递不匹配的playervector<monster>。 您可以做的是将模板更改为:

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

这将允许您获取相同类型或其他类型的对象和向量。