传递单个或向量对象时没有重载函数的实例
No instance of overloaded function when passing single or vector of objects
我正在尝试将一个对象和类对象的向量或两个对象传递给两个名为 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 );
表示您需要一个对象和一个相同类型的对象的向量。 您正在向函数传递不匹配的player
和vector<monster>
。 您可以做的是将模板更改为:
template<class T, class Y> bool checkPosition( const T &object1, const std::vector<Y> &object2 );
这将允许您获取相同类型或其他类型的对象和向量。
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 使用模板重载函数
- C++线程中,没有重载函数接受 X 参数
- std::vector 没有重载函数的实例与参数列表匹配
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 对重载函数find_first_not_of的不明确调用
- 如何从重载解析中删除重载函数?
- CUDA:重载函数"isnan"的多个实例
- C++派生类重载函数(带有 std::function 参数)不可见
- 避免在人为的重载函数调用中拼写出类型
- C++:如何为多个重载函数保留通用代码路径?
- 什么时候可以使用常量装饰调用我的重载函数?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- std::调用,未找到匹配的重载函数
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 如何通过签名作为模板参数来解决重载函数?