gpt4 book ai didi

c++ - error LNK2019 未解析的外部函数

转载 作者:行者123 更新时间:2023-11-28 06:37:23 24 4
gpt4 key购买 nike

当我编译我的程序时,我得到: errors

程序工作,如果我评论这些部分(功能车辆::驱动器):

if (check_position(pos, number, 1, 0))
continue;
change_position(pos, number, 1, 0);

函数 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;
}

Function vehicles::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);

最佳答案

问题是您的声明与定义不匹配。声明通过值获取第一个参数,而定义通过引用获取它。在 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);

关于c++ - error LNK2019 未解析的外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com