c++ - 无法将某些函数从一个文件移动到另一个 C++

标签 c++ header g++

我正在从事我的 C++ 类(class)中的一个项目。我提交了之前的作业,我在其中实现了 Hex 游戏的命令行版本,并使用最小的 AI 与玩家对战。对于我目前的任务,我正在用一些蒙特卡罗的东西来扩展人工智能。为此,我正在尝试将 AI 决策移动到它自己的单独类中(它最初只是作为包含 main 的文件中的函数调用)。 G++ 给我带来了我不明白的可怕的编译器错误,所以希望有人能指出我的语法错误在哪里,因为我是 C++ 的新手并且无法找到它们,而且 g++ 错误并不是特别有用时间。 HexAI.cpp 中函数的内容应该不是问题,因为它们是从原始位置复制粘贴的,而且我还移动了它们引用的全局变量。我唯一添加的是类定义、构造函数/析构函数和 HexAI::getMove() 函数。 vector 板声明是一个二维整数 vector ,如果位置被占用,则用于保存 0,如果玩家 1 拥有它,则为 1,如果玩家 2 拥有,则为 2。预先感谢您提供的所有帮助,并且由于我是新手,请随时指出我遗漏的任何细节,如您认为有必要。我只想了解发生了什么并继续前进,因为我整个下午都被困在这个问题上。

HexAI.h

#include <vector>
#ifndef HEXAI_H
#define HEXAI_H


typedef vector<int> intvec;

class HexAI{
    public:

    HexAI(int s);
    ~HexAI();
    int* getMove(vector<intvec> board);

    private:

    void AIRandomMove(vector<intvec> board);
    void AI (vector<intvec> board);

    int compMadeMove; //decides if it is the first turn or not
    int compMoveI; //row
    int compMoveJ; //column
    int size; //size of the board
};

#endif

HexAI.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "HexAI.h"

using namespace std;

typedef enum playerColor{
    neutral, p1, p2
}playerColor;

typedef vector<int> intvec;

HexAI::HexAI(int s){
    compMoveI = 0;
    compMoveJ = 0;
    compMadeMove = false;
    size = s;
    srand(time(NULL));
}

~HexAI::HexAI(){
    //nothing needs to be deleted
}


//when it throws its hands in the air
void HexAI::AIRandomMove(vector<intvec> board){
    bool acceptableMove = false;
    while( not acceptableMove){
        int a = rand() % (size - 1);//it has trouble when its randomly on the bottom
        int b = rand() % size;
        if(board[a][b] == neutral){
            acceptableMove = true;
            compMoveI = a;
            compMoveJ = b;
        }
    }
}


//makes a decision based on the board, sets 2 variables
//that are read in as the move later on
//tries to go straight top to bottom, move left or right if that spot is taken.
//goes for a random move if it can't decide how to move
void HexAI::AI (vector<intvec> board){
    //catch seg faults that show up 1 turn after random indexes put it on the bottom row
    if(compMadeMove and compMoveI == (size - 1) ){ //if it randomly ends up on the bottom, move it to the top and start over, here to fix seg faults
        compMadeMove = false;
    }
    //determine starting position
    if( not compMadeMove ){//first move, pick a spot on the top
        bool goodguess = false;
        int startPos = rand() % size;
        while(!goodguess){
            if(board[0][startPos] != 0){
                startPos = rand() % size;
            }else{
                goodguess = true;
            }
        }
        compMoveI = 0;
        compMoveJ = startPos;
        compMadeMove = true;
    }else{//later moves
        int potentialMove = board[compMoveI + 1][compMoveJ];
        if(potentialMove == neutral){//try to just move down one
            compMoveI += 1;
        }else{//try downleft or just left or right
            potentialMove = board[compMoveI + 1][compMoveJ - 1];
            if(potentialMove == neutral){//downleft
                compMoveI += 1;
                compMoveJ -= 1;
            }else{//downLeft was taken, go left or right
                potentialMove = board[compMoveI][compMoveJ + 1];
                if(potentialMove == neutral){//go right
                    compMoveJ += 1;
                }else{//hopefully left works?
                    potentialMove = board[compMoveI][compMoveJ - 1];
                    if(potentialMove == neutral){
                        compMoveJ -= 1;
                    }else{//ARG I give up, random
                        AIRandomMove(board);
                    }
                }
            }
        }
    }
    if(compMoveI < 0 or compMoveI >= size or compMoveJ < 0 or compMoveJ >= size){
        AIRandomMove(board);
    }
    cout << endl << "My move is " << compMoveI << " " << compMoveJ << endl << endl;
}

int* HexAI::getMove(vector<intvec> board){
    AI(board);
    int* ij = new int[2];
    ij[0] = compMoveI;
    ij[1] = compMoveJ;
    return ij;
}

g++ 错误

g++ -c -Wall HexAI.cpp
In file included from HexAI.cpp:5:
HexAI.h:6: error: expected initializer before â<â token
HexAI.h:13: error: expected â;â before â(â token
HexAI.h:17: error: âvectorâ has not been declared
HexAI.h:17: error: expected â,â or â...â before â<â token
HexAI.h:18: error: âvectorâ has not been declared
HexAI.h:18: error: expected â,â or â...â before â<â token
HexAI.cpp:23: error: expected constructor, destructor, or type conversion before â::â token
HexAI.cpp:29: error: prototype for âvoid HexAI::AIRandomMove(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â does not match any in class âHexAIâ
HexAI.h:17: error: candidate is: void HexAI::AIRandomMove(int)
HexAI.cpp:47: error: prototype for âvoid HexAI::AI(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â does not match any in class âHexAIâ
HexAI.h:18: error: candidate is: void HexAI::AI(int)
HexAI.cpp:96: error: no âint* HexAI::getMove(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â member function declared in class âHexAIâ
make: *** [HexAI.o] Error 1

最佳答案

在 header 中的所有 vector 声明前添加 std:: 将修复大部分错误,例如:

typedef vector<int> intvec;

应该是:

typedef std::vector<int> intvec;

您在析构函数实现中混淆了 ~,这是:

~HexAI::HexAI(){

应该是:

 HexAI::~HexAI(){

我不鼓励使用 using namespace std; 可以节省您的输入时间,但以后可能会给您带来问题。

关于c++ - 无法将某些函数从一个文件移动到另一个 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906681/

相关文章:

php Header 重定向后重定向问题

powershell - 在Powershell中从CSV删除标题

c++ - 不规则的 Makefile 行为?

c++ - Visual C++ 2008 和 g++ 的区别

c++ - -Wundef 不会被 g++ 中的 pragma 忽略

c++ - 将 vector 的 vector 转换为指针的指针

php - 在 PHP/C++ 中加密自定义数据包

c++ - C/C++ 中的运算符优先级和关联性

c++ - 如何在c++中包含头文件?

c++ - 类的前向声明在 C++ 中似乎不起作用