.o文件中找不到的函数,没有编译器错误

我正在使用类(MyGame,Board,Player,Dice)进行此蛇形和梯形程序。当我构建它时,没有编译器错误,尽管我确实在控制台中得到了它

"/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/joshu/AppData/Local/Temp/cczkm3rv.o: in function MyGame::play(Player*, Dice, Board)':c:\Users\joshu\Documents\VSC\OOP\src\MyGame.cpp:33: undefined reference toMyGame::getNumPlayers()' c:\Users\joshu\Documents\VSC\OOP\src/MyGame.cpp:33:(.text+0x370): relocation truncated to fit: R_X86_64_PC32 against undefined symbol MyGame::getNumPlayers()' /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: c:\Users\joshu\Documents\VSC\OOP\src/MyGame.cpp:40: undefined reference toBoard::getReward()' c:\Users\joshu\Documents\VSC\OOP\src/MyGame.cpp:40:(.text+0x483): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Board::getReward()' /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: c:\Users\joshu\Documents\VSC\OOP\src/MyGame.cpp:42: undefined reference toBoard::getReward()' c:\Users\joshu\Documents\VSC\OOP\src/MyGame.cpp:42:(.text+0x4ec): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Board::getReward()' /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libcygwin.a(libcmain.o): in functionmain':
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to WinMain' /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbolWinMain' collect2: error: ld returned 1 exit status <-----------------------searched line The terminal process terminated with exit code: 1

终端将被任务重用,按任意键将其关闭。”

当我尝试调试时,出现一个弹出窗口,提示无法找到skanes.exe(skanes.cpp是我的主文件夹)。 我在google中搜索了一下,发现那是一个内联错误,链接器无法在.o文件中找到此类代码 我什至不知道.o文件在哪里,尽管我确实知道task.json提到了.o文件,因为我必须编辑该文件(使用教授的特定说明)才能使调试器工作。task.json和launcher.json位于名为.vscode的文件夹中,该程序的.cpp和.h文件也与.vscode放在同一文件夹中。抱歉,如果不添加所需的文件来获取解决方案,那只是我不知道wtf是链接器还是the.o文件,所以我不知道它们是如何工作的。

我正在使用VS Code,以防万一。

task.json

"version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "C:\\cygwin\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "${fileDirname}\\MyGame.cpp",
                "${fileDirname}\\Board.cpp",
                "${fileDirname}\\Player.cpp",
                "${fileDirname}\\Dice.cpp",
                "-I",
                "${fileDirname}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\cygwin\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

launch.json

"version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\cygwin\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

MyGame.h

#ifndef MYGAME_H
#define MYGAME_H
#include "Board.h"
#include "Player.h"
#include "Dice.h"

#include "Player.h"
class MyGame
{

    protected:
        static const int numPlayers = 2;

    public:
        MyGame();
        ~MyGame();
        void start();
        void play(Player[], Dice, Board);  <-------Line 18
        void win(Player[]);                <-------Line 19
        int getNumPLayers();
};

#endif

MyGame.cpp

#include <iostream>
#include <vector>
#include "MyGame.h"
#include "Board.h"
#include "Player.h"
#include "Dice.h"

MyGame::MyGame()
{

}

MyGame::~MyGame()
{

}

void MyGame::start()
{
    Board brd;
    Player plyr[numPlayers];
    Dice dc;
    while (plyr[0].getPosition() != brd.getBoardSize() && plyr[1].getPosition() != brd.getBoardSize() && plyr[numPlayers - 1].getTurn() <= plyr[numPlayers - 1].getMaxTurn())
        play(plyr, dc, brd);

    win(plyr);

}

void MyGame::play(Player p[], Dice d, Board b)
{

        for (int i = 0; i < b.getBoardSize(); i++)
        {
            p[i].setPosition(d.roll());
            if(p[i].getPosition() > b.getBoardSize())
            {
                p[i].setPosition( (b.getBoardSize() - p[i].getPosition()) * 2 );
            }

            if (b.getType(p[i].getPosition()) == 'S')
                p[i].setPosition(-b.getSnakeLadderMove());
            else if (!b.getType(p[i].getPosition()) == 'L')
                p[i].setPosition(b.getSnakeLadderMove());

            p[i].setTurn();
        }

}

void MyGame::win(Player p[])
{
    for (int i = 0; i > numPlayers; i++) 
    {
        if (p[i].getPosition() == 30)
            std::cout << "Payer " << i << "wins!!" << std::endl;
    }
}

董事会

#ifndef BOARD_H
#define BOARD_H
#include "MyGame.h"

class Board
{
    public:
        Board();
        ~Board();
        bool getType(int);
        int getNumeber(int);
        int getSnakeLadderMove();
        int getBoardSize();

    private:
        struct tile
        {
            char type;
            int number;
        };

        static const int boardSize = 30;
        static const int snakeLadderMove  = 3;
        tile place[boardSize];
};

#endif

Board.cpp

#include <stdlib.h>
#include <time.h>
#include "Board.h"

Board::Board()
{
    int count = 0;
    //initialize random seed to randomize snakes and ladders.
    srand(time(NULL));

    for (int k = 0; k < boardSize; k++)
    {
        place[k].type = 'N';
        place[k].number = k + 1;
    }

    while(count <= 3)
    {
        int index = rand() % boardSize + 1;

        while (index < 4)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N'
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }

        place[index].type = 'S';

        while (index > boardSize - 3)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N' 
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }   

        place[index].type = 'L';
        count++;

    }
}

Board::~Board()
{

}

int Board::getNumeber(int index)
{
    return place[index].number;
}

bool Board::getType(int index)
{
    return place[index].type;
}

int Board::getBoardSize()
{
    return boardSize;
}

播放器

#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"
#include "Board.h"

class Player
{
    public:
        Player();
        ~Player();
        void setPosition(int);
        void setTurn();
        int getPosition();
        int getTurn();
        int getMaxTurn();
        int getNumPlayers();

    private:
        static const int maxTurn = 20;
        int position;
        int turn;
};

#endif

Player.cpp

#include <iostream>
#include "Player.h"
#include "Board.h"

Player::Player()
{

    /* 
        In order for the setters to work position and turn
        have to be equal to 1;
    */
    position = 1;
    turn = 1;
}

Player::~Player()
{

}

void Player::setPosition(int move)
{
    //Assumes constructor setted the value to 0
    position += move;
    ;
}

void Player::setTurn()
{
    //Assumes constructor sette4d the value to 0
    turn++;
}


int Player::getPosition()
{
    return position;
}

int Player::getTurn()
{
    return turn;
}

int Player::getMaxTurn()
{
    return maxTurn;
}

骰子

#ifndef CDADO_H_INCLUDED
#define CDADO_H_INCLUDED

#include <ctime>
#include <cstdlib>

class Dice{

    public:

        Dice();
        int roll();

};


#endif

Dice.cpp

#include <iostream>
#include "Dice.h"

using namespace std;


Dice::Dice()
{
    srand(time(0));
}


int Dice::roll()
{
    return  (rand() % 6) + 1;
}  

skanes.cpp //应该是蛇。

#include <iostream>
#include "MyGame.h"

using namespace std;

int main()
{
    MyGame snakes;

    snakes.start();
}