1

Greetings.

I have searched for a solution, but I think this problem is personal code specific, hence my posting here.

I'll get straight to the point.

In my main I have two objects.

Computer *computer = new Computer();
Player *player = new Player();

In the computer class, in the header I have the following:

  private:

Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;

Then in Computer.cpp:

Computer::Computer(char token, bool hasTurn)
{
    m_token = token;
    m_hasTurn = hasTurn;
    strategy = new Strategy();
}

int Computer::getMove(const char (&board)[9])
{
    twoInRow = strategy->checkTwoInRow(board);
    counter = strategy->counter(board);
    makeTwo = strategy->makeTwo(board);

    if(twoInRow != 0)
    {
        return twoInRow - 1;
    } else if(counter != 0) {
        return counter - 1;
    } else if(makeTwo != 0) {
        return makeTwo - 1;
    } else {
        return 0;
    }
}

At this point I think the problem arises.

The methods called from the class Strategy all require knowledge of the board thus:

int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);

The problems im getting, unabling them to compile:

Error   1   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

Error   2   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

Error   3   error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow@Strategy@@QAEHAAY08$$CBD@Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove@Computer@@QAEHAAY08$$CBD@Z)    C:\CPP\TTT\Computer.obj tictactoeCPP

As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.

Can anyone explain WHY this error is occuring, I don't quite understand the error at all. And also how to solve/prevent this?

EDIT*

I just got a request to share the Strategy class:

Strategy.h:

    #pragma once
class Strategy
{
public:
    Strategy(void);
    ~Strategy(void);

    int checkTwoInRow(const char (&board)[9]);
    int counter(const char (&board)[9]);
    int makeTwo(const char (&board)[9]);
};

The class defined these methods, I won't post them because they are quite long.

3
  • Show us the Strategy class and it's descendent's, I think that's where your problem lives. Commented Apr 13, 2011 at 10:08
  • @Tony Will do. Thanks for the notice. Commented Apr 13, 2011 at 10:28
  • Also tried changing #pragma once to #ifndef STRATEGY_H and #define STRATEGY_H (altough i dont really understand this yet, thats just what i use in other headers) and it doesnt change anything ._. Commented Apr 13, 2011 at 10:35

2 Answers 2

9

This is a linking error and it has nothing to do with instantiations or parameters.

You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.

Or did you perhaps define them like this:

int checkTwoInRow(const char (&board)[9])
{
   // Do something with board the wrong way
}

instead of like this:

int Strategy::checkTwoInRow(const char (&board)[9])
{
   // Do something with board the right way
}

The first one doesn't define a Strategy member function, it defines a global function.

Sign up to request clarification or add additional context in comments.

3 Comments

I defined them using the second version: Strategy::checkTwoInRow. Thanks for your explanation. The problem is more then clear :) Thanks you. If you dont mind, i'd like to ask you one more thing though. Is that what #ifndef and #define does?
Okay Just found the problem. I was quite stupid, I indeed used your first version. Forgot to use Strategy::... even after beign sure I did, because I always do on class files... but this time I just purely forgot. Thanks alot.
@Joey Roosing : That's what I was referring to by 'accidentally defining them as free functions'. :-]
3

The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?

3 Comments

Actually, I have not defined them at all in the header. need to define Strategy::counter in the Computer header?
@Joey Roosing : Be certain that you understand the difference between a declaration and a definition. You showed their declarations in your original question (presumably that was an excerpt from class Strategy), but where are those member functions actually implemented?
They are implemented in the Strategy class and all three of them return an integer. And yes you are right. I mixed up defiinition and declaration. Sorry about that. the methods are defined and declared in the Strategy class and I need to access them in the computer class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.