gpt4 book ai didi

c++ - 编译语法错误

转载 作者:行者123 更新时间:2023-11-30 04:05:04 24 4
gpt4 key购买 nike

我正在尝试编译纸牌游戏项目。不幸的是,我遇到了“预期的;之前( token ”)错误,我无法理解。player 头文件中出现错误,突出显示。

//player.h
#ifndef PLAYER
#define PLAYER
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "card.h"


using std::vector;

class player{
public:
card getHand(int);
vector <card> gethand();
void printhand();
int gethandsize();
int givecard(deck&,int,int); //**ERROR OCCURS HERE**
bool checkdecision(int* decision);
bool compare(int decision, player& thief);
void steal(card);
void discard(int decision);

protected:



private:
vector <card> h;
};
#endif

//player.cpp
#include "player.h"
player::player(){
}

vector <card> player::gethand(){
return h;
}

card player::getHand(int num){
return h[num];
}

int player::givecard(deck& pool, int hand_index, int &deck_index){
card temp = pool.getd(deck_index);
h.push_back(temp);
pool.pop_back();
}

void player::printhand(){
for(int a = 0; a<h.size(); a++){
cout << h[a].rank_map << " of " << h[a].suit_map << endl;
}
cout << endl;
}


bool player::checkdecision(int decision){
int counter = 0;
for(int a=0; a<h.size(); a++){
if(h[a].rank != decision){
++counter;
}
}
if(counter == h.size()){
cout << "You don't have the card. Start again." << endl;
return true;
}
return false;
}

bool player::compare(int decision, player& thief){
card temp;
int index = 0, initial_hand = h.size();
for(int b = 0; b < initial_hand; b++){
if(h[b].rank != decision){
index++;
}
}
for(int i = 0; i < h.size(); i++){
if(h[i].rank == decision){
cout << "Steal!" << endl;
temp = h[i];
thief.steal(temp);
h.erase(h.begin() + i);
i--;
}
}
if(index == initial_hand){
return false;
}
else if (index != initial_hand){return true;}
}

void player::discard(int decision){
int counter = 0;
for(int k = 0; k < h.size(); k++){
if(h[k].rank == decision){
counter++;
}
}
if(counter == 4){
for(int a = 0; a < h.size(); a++){
if(h[a].rank == decision){
h.erase(h.begin() + a );
a--;
}
}
}
}

void player::steal(card temp){
h.push_back(temp);
}
int player::gethandsize(){
return h.size();
}

player::~player(){}

//cardgames.h
#ifndef CARD_GAMES
#define CARD_GAMES
#include "player.h"
#include "deck.h"



class card_games{
public:
card_games();
void play();
void checkplayer(char thePlayer);
int convert(char);
void distribute();
void setgame(int);
void printhand();
deck getpool();
player getplayer(int);
~card_games();

protected:
int num_player; //number of players
int player_num; //player's turn

private:
deck pool; //pool object
player* p; //array of players
};
#endif

//cardgames.cpp
#include "card_games.h"
#include <iostream>
#include <vector>

using namespace std;

card_games::card_games(){}

void card_games::play(){
char thePlayer;
int temp;
cout << "How many players are playing?" << endl;
cin >> thePlayer;
checkplayer(thePlayer);
temp = convert(thePlayer);
setgame(temp);
distribute();
}

void card_games::checkplayer(char thePlayer){
if(thePlayer < 50 || thePlayer > 54 ){
cout << "Invalid number of players. Please retry." << endl;
cout << endl;
exit(1);
}
}

int card_games::convert(char input){
int value;
player_num = 1;
if(input== '2'){
value = 2;
}
if(input== '3'){
value =3;
}
if(input== '4'){
value = 4;
}
if(input== '5'){
value = 5;
}
if(input == '6'){
value= 6;
}
return value;
}

void card_games::setgame(int temp){
p = new player[temp];
num_player = temp;
}

void card_games::distribute(){
int temp;
int deck_index = 51;
for(int a=0; a < num_player; a++){ //player number
for(int b=0; b < 7; b++){ // hand size
p[a].givecard(pool, b, deck_index);
--deck_index;
}
}
}

player card_games::getplayer(int temp){
return p[temp];
}

deck card_games::getpool(){
return pool;
}

card_games::~card_games(){
}

感谢任何输入。

最佳答案

givecard 的原型(prototype)与声明的方法不匹配:

 int givecard(deck&,int,int);

应该是:

 int givecard(deck&,int,int&);

希望这对您有所帮助。

关于c++ - 编译语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com