C++ 传递单个或向量对象时没有重载函数的实例
我试图将一个对象和一个类对象向量或两个对象传递给两个名为checkPosition的重载模板函数。C++ 传递单个或向量对象时没有重载函数的实例,c++,function,templates,object,overloading,C++,Function,Templates,Object,Overloading,我试图将一个对象和一个类对象向量或两个对象传递给两个名为checkPosition的重载模板函数。 无论我尝试什么,将我的函数更改为按值传递/引用/指针/常量/非常量,它都会给我错误 错误:重载函数“Room::checkPosition”的实例与参数列表不匹配 参数类型有:(常量玩家,常量std::vector
无论我尝试什么,将我的函数更改为按值传递/引用/指针/常量/非常量,它都会给我错误
错误:重载函数“Room::checkPosition”的实例与参数列表不匹配
参数类型有:(常量玩家,常量std::vector
对象类型为:const 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;
};
教室
{
公众:
玩家;
病媒怪物;
向量墙;
std::向量出口;
模板void xSet(T1&对象,const int&输入);
模板void ySet(T2&对象、常量int&输入);
模板int x(T3和对象);
模板int y(T4和对象);
模板布尔检查位置(常数T5和对象1、常数T5和对象2);
模板bool checkPosition(常量T6和object1,常量std::vector和object2);
void objecticset();
无效长度集(常数输入和输入);
空隙宽度集(常数输入和输入);
int-length();
int宽度();
void staticDataMap();
void completeDataMap();
字符静态数据映射(intx,inty);
字符完成数据映射(int x,int y);
私人:
整数长度;
内部宽度;
std::vector\u staticDataMap;
std::vector\u completeDataMap;
};
从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;
}
模板文件室::检查位置(常数T5和对象1、常数T5和对象2)
{
if(object1.position.x==object2.position.x&&
object1.position.y==object2.position.y)
{
返回真值
}
返回false;
}
模板文件室::检查位置(常数T6和对象1,常数标准::向量和对象2)
{
for(inti=0;i
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 );
}
}
}
}
bool checkWinCondition(const Room&Room)
{
if(room.checkPosition(room.player,room.exit)=true)
{
std::cout您正在使用两个不同的参数调用函数,但函数模板化为一种类型
template<class T6> bool checkPosition( const T6 &object1, const std::vector<T6> &object2 );
这将允许您获取一些对象和相同类型或其他类型的向量。仅供记录:您将遇到的下一个问题。您正在对const
对象调用非const
函数。此外,您不必在每个函数中使用不同的模板类型名(T1
,T2
,…)。你可以只使用T
。你说向量中的类型与第一个参数的类型相同。当然,作为第一个参数的Player
,作为向量中的类型的Monster
,是不起作用的。它们与你说的不一样。这就解决了问题,并删除了常量作为参数我的印象是一个模板类型可以是多种类型,而我所做的事情只需要一种类型,我想我错了。
template<class T, class Y> bool checkPosition( const T &object1, const std::vector<Y> &object2 );