无法将某些功能从一个文件移动到另一个C ++

时间:2013-06-03 22:10:31

标签: c++ header g++

我正在研究我的C ++课程中的一个项目。我转而执行了之前的任务,我实现了游戏Hex的命令行版本,并使用最小的AI来对抗玩家。对于我目前的任务,我正在用一些蒙特卡罗的东西扩展人工智能。为此做准备,我试图将AI决策移动到它自己独立的类中(它最初只是作为包含main的文件中的函数调用)。 G ++给了我可怕的编译器错误,我不明白,所以希望有人可以指出我的语法错误在哪里,因为我是C ++的新手并且无法跟踪它们,并且g ++错误并不是特别有用时间。 HexAI.cpp中函数的内容不应该是问题,因为它们是从原始位置复制粘贴的,我也移动了它们引用的全局变量。我添加的唯一内容是类定义,构造函数/析构函数和HexAI :: getMove()函数。向量板声明是一个2d的整数向量,用于在拍摄点时保持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

3 个答案:

答案 0 :(得分:3)

在标头中的所有std::声明前添加vector将解决大部分错误,例如:

typedef vector<int> intvec;

应该是:

typedef std::vector<int> intvec;

你在析构函数实现中混淆了~,这个:

~HexAI::HexAI(){

应该是:

 HexAI::~HexAI(){

我不鼓励使用using namespace std;可以节省你一点点打字,但以后会引起你的问题。

答案 1 :(得分:0)

更改

typedef vector<int> intvec;

typedef std::vector<int> intvec;

答案 2 :(得分:0)

  • HexAI.h中没有using namespace std,所以它不知道“vector”是什么。您应该使用std::vector代替。
  • ~HexAI::HexAI应为HexAI::~HexAI

否则应编译;希望我能提供帮助。