Kulis
Kulis

Reputation: 1010

error LNK2019 unresolved external function

When i compile my program, I get: errors

Program work, if I comment these section (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;
}

Function vehicles::drive (mechanics.cpp), unfinished but work after comment above code:

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

class vehicles (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"; }
};

Declaration of these function:

void change_position(vehicles::position, int, int, int);
bool check_position(vehicles::position, int, int, int);

Upvotes: 0

Views: 62

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20063

The problem is your declarations do not match the definitions. The declarations take the first argument by value where the definitions take it by reference. In C++ these must match exactly otherwise the linker will not know which one use when function overloading is used. You can fix this easily by updating the declarations to the following

void change_position(vehicles::position&, int, int, int);
//                                     ^
bool check_position(vehicles::position&, int, int, int);
//                                    ^

I recommend that you change the check_position function to take the first argument by const reference since the function does not modify it.

bool check_position(const vehicles::position&, int, int, int);

Upvotes: 1

Related Questions