0

I start learning C++ in school and this error appear.

1>Bettle_Dice.obj : error LNK2019: unresolved external symbol "public: int __thiscall Beetle::checkcom(void)" (?checkcom@Beetle@@QAEHXZ) referenced in function _main

I have include other header files and cpp files, I don understand why only this file have problem please help

Below is my code main.cpp

#include "stdafx.h"
#include "beetle.h"
#include "dice.h"
#include "player.h"
#include <iostream>

using namespace std;


int main()
{
    Player p;   //declare Class to a variable
    Dice d;
    Beetle btle;
    int temp;
    cout << "Number of players?" << endl;
    cin >> temp;

    p.Num(temp); //store the number of player into class
    //cout << p.getNumPlayers() <<endl;

    cout << "Start game!!" <<endl;
    temp = btle.checkcom();
    while(temp != 1)
    {
        for(int i=0;i<p.getNumPlayers();i++)
        {
            temp = d.roll();
            cout <<"Your roll number:"<< temp;

        }

    }


    return 0;
}

beetle.h

class Beetle
{
    private:
        int body,head,ante,leg,eye,tail;
    public:
        int completion();
        int checkcom();
        int getBody() { return body;};
        int getHead() { return head;};
        int getAnte() { return ante;};
        int getLeg() { return leg;};
        int getEye() { return eye;};
        int getTail() { return tail;};
};

Beetle.cpp

#include "stdafx.h"
#include "beetle.h"

int completion()
{
    return 0;
}

int checkcom()
{
    Beetle btle;
    int flag = 0;
    if(btle.getBody() == 1 && btle.getHead() == 1 && btle.getAnte() == 2 && btle.getEye() == 2 && btle.getLeg() ==6 && btle.getTail() == 1)
        flag = 1;
    return flag;
}

I checked some solution on the internet, some are saying is the library problem, but this file is not a built-in function. I guess it is not the problem of the library. I tried to include the beetle.obj file to it and the debugger said it is included already and duplicate definition. In the other file, i do not have "bettle" this word. It should not be the problem of double declaration or duplicate class. I have no idea what the problem is. Please help.

  • You might want to check out a beginners book or tutorial from [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Nov 07 '15 at 16:07
  • After some reading, I notice I made a really simple mistake. I forget to add the prefix before the function in the cpp file. Thank you. – Sing Nov 07 '15 at 17:38

1 Answers1

2

You need to prefix the signature of your class functions with the class name Beetle::

Otherwise the compiler just thinks those functions are global functions, not member functions.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21