当我编译我的程序时,我得到:
编程工作,如果我评论这些部分(function vehicles :: drive):
if (check_position(pos, number, 1, 0))
continue;
change_position(pos, number, 1, 0);
Fuction change_position& check_position(terrain.cpp):
void change_position(vehicles::position &pos, int number, int vertical, int horizontal){
pos.x[number] = pos.x[number] + vertical;
pos.y[number] = pos.y[number] + horizontal;
}
bool check_position(vehicles::position &pos, int number, int vertical, int horizontal)
{
if (pos.x[number] + vertical > MAX_SIZE || pos.x[number] + vertical < 0)
return true;
if (pos.y[number] + horizontal > MAX_SIZE || pos.y[number] + horizontal < 0)
return true;
return false;
}
功能车辆:: drive(mechanics.cpp),未完成但在上述代码评论后工作:
void vehicles::drive(int move, vehicles::position &pos, int number)
{
int direction;
cout << "Press W,A,S,D to move or Q to quit\n\n";
while (move)
{
if (move <= 0)
break;
cin >> direction;
switch (direction)
{
case 'w':
case 'W':
if (check_position(pos, number, 1, 0))
continue;
change_position(pos, number, 1, 0);
--move;
break;
case 'q':
case 'Q':
break;
default:
cout << "Press W,A,S,D to move or Q to quit\n\n";
break;
}
}
级车辆(vehickles.h):
class vehicles{
protected:
double durability;
double velocity;
public:
vehicles(double d, double v) : durability(d), velocity(v) {}
~vehicles() {}
struct position{
vector<int> x;
vector<int> y;
}pos;
void drive(int move, position &pos, int number);
void info() { cout << durability << " " << velocity << "\n"; }
};
声明这些功能:
void change_position(vehicles::position, int, int, int);
bool check_position(vehicles::position, int, int, int);
答案 0 :(得分:1)
问题是您的声明与定义不匹配。声明采用第一个参数,其中定义通过引用获取它。在C ++中,这些必须完全匹配,否则链接器将不知道在使用函数重载时使用哪一个。您可以通过将声明更新为以下
来轻松解决此问题void change_position(vehicles::position&, int, int, int);
// ^
bool check_position(vehicles::position&, int, int, int);
// ^
我建议您更改check_position
函数以通过const
引用获取第一个参数,因为该函数不会修改它。
bool check_position(const vehicles::position&, int, int, int);