1

This problem has been solved before, but I've been looking all over, and none of those explains how to fix this, the situation i'm in. Most of them is about external libraries.

I'm trying to test my code. I've made a test class and that class is trying to access another class by including that class's header file. But when I'm trying to call its function it just gives me an unresolved external symbol error.

This is my current attempt. Here I'm trying to access the other classes's header file to call it's functions.

CardTest.cpp

#include <iostream>
#include "../Header Files/Hand.h"
#include "../Header Files/HandValueCalculator.h"

using namespace std;

HandValueCalculator handValueCalculator;
Hand hand;

void Test() {
    bool value = handValueCalculator.DoesHandHaveAce(&hand.cards);
    cout << value << endl;
}

HandValueCalculator.h

#ifndef HANDVALUECALCULATOR_H_INCLUDED
#define HANDVALUECALCULATOR_H_INCLUDED

#include <vector>
#include "../Header Files/Card.h"

class HandValueCalculator {
public:
    HandValueCalculator();
    bool DoesHandHaveAce(std::vector<Card>* cards);
    int GetValueWithoutAce(std::vector<Card>* cards);
    int GetValueWithAce(std::vector<Card>* cards);
};

#endif // HANDVALUECALCULATOR_H_INCLUDED

HandValueCalculator.cpp

#include "../Header Files/HandValueCalculator.h"

HandValueCalculator::HandValueCalculator() {

}

bool HandValueCalculator::DoesHandHaveAce(std::vector<Card>* cards) {
    int i;
    for (i = 0; i < cards.size(); i++) {
        if (cards.at(i).GetValue() == 11) {
            return true;
        }
    }
    return false;
}

int HandValueCalculator::GetValueWithoutAce(std::vector<Card>* cards) {
    for (i = 0; i < cards.size(); i++) {
        int cardValue = cards.at(i).GetValue()
            totalValue = totalValue + cardValue;
    }
    return 0;
}

int HandValueCalculator::GetValueWithAce(std::vector<Card>* cards) {
    return 0;
}

This is the error I'm getting, and I don't think the compiler recognizes that the functions have a body, and because it can't find a body for the declared functions it returns an error like this.

C:\Users\fagel\Documents\Blackjack\Blackjack\CardTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall HandValueCalculator::a(void)" (?a@HandValueCalculator@@QAEXXZ) referenced in function "void __cdecl Test(void)" (?Test@@YAXXZ)

PlaZinumia
  • 25
  • 3

1 Answers1

0

Your HandValueCalculator does not have a void a(); implementation available to the linker. If the a function is defined, make sure you link with the object file containing the definition.

However, you're most likely the victim of the most vexing parse and think you've declared a to be a variable (somewhere not shown), but you've instead declared a function (without definition).

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • And how would one link with the object file containing the definition? – PlaZinumia Jun 16 '19 at 01:16
  • By making sure the linker has all the object files it needs. Examine the linking phase. Does the object file containing the compiled definition of `void __thiscall HandValueCalculator::a(void)` appear? – Ted Lyngmo Jun 16 '19 at 01:20