gpt4 book ai didi

C++ 错误 1 ​​错误 C2146 : syntax error: missing ';' before identifier 'records'

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:14 25 4
gpt4 key购买 nike

井字棋.h

#pragma once
#include "Header.h"

class TicTacToe
{
vector<char>tictac_array;
Records records;
public:
TicTacToe();

~TicTacToe();

void push_tictac_array();

int wincheck();

void board(string player1, string player2);

void start();

void mainmenu();
};

标题.h

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
#include "Records.h"
#include "TicTacToe.h"

using namespace std;

记录.h

#pragma once
#include "Header.h"

class Records
{
protected:
vector<Records*> rec;
string player;
int wins;
int games;
int draws;
public:
Records();
Records(string player, int wins, int games, int draws);

~Records();

void setName(string player);

void setWins(int wins);

void setGames(int games);

void setDraws(int draws);

string getName();

int getWins();

int getGames();

int getDraws();

void addRec(string player, int wins, int games, int draws);

void sortStats();

void outRec();

void saveTo();

void readFrom();
};

主要.cpp

#include "Header.h"

const int NotUsed = system("color F0");


void main()
{
setlocale(0, "rus");
TicTacToe tictac;
tictac.records.readFrom();
tictac.mainmenu();
}

在文件崩溃之前一切正常。只有传播所有类文件后,才开始发出此错误,可能是什么问题?添加了 Records.h 头文件

最佳答案

有多少困惑。

首先:

  • never use useless include in header files.
  • never use namespace in header files.

TicTacToe.h 中,您包含 "Header.h",其中包含 "TicTacToe.h" 本身:递归。

第二:

"#pragma once" is not standard, so I recommend to use instance:

#ifndef RECORDS_H_
#define RECORDS_H_

class Records {
}

#endif

如果你真的想用"#pragma once" , 学习使用它之前:它应该是第一条指令,至少在所有“includes”之前,一般来说。

第三:

  • 使用关键字“private”,即使它是隐含的;
  • get方法应该是const;
  • 通过引用传递参数,避免无用的复制。

井字棋.h

#ifndef TICTACTOE_H_
#define TICTACTOE_H_

#include <vector>

#include "Records.h"

class TicTacToe {
private:
std::vector<char> tictac_array;
Records records;
public:
TicTacToe();
~TicTacToe();
void push_tictac_array();
int wincheck();
void board(const std::string& player1, const std::string& player2);
void start();
void mainmenu();
};

#endif

记录.h

#ifndef RECORDS_H_
#define RECORDS_H_

#include <vector>

class Records {
protected:
std::vector<Records*> rec;
string player;
int wins;
int games;
int draws;
public:
Record();
Record(const std::string& player, const int& wins, const int& games,const int& draws);
~Record();
void setName(const std::string& player);
void setWins(const int& wins);
void setGames(const int& games);
void setDraws(const int& draws);
string getName() const;
int getWins() const;
int getGames() const;
int getDraws() const;
void addRec(const std::string& player, const int& wins, const int& games,const int& draws);
void sortStats();
void outRec();
void saveTo();
void readFrom();
};

#endif

主要内容:

#include <clocale>
#include <cstdlib>

#include "TicTacToe.h"

using namespace std;

const int NotUsed = system("color F0");

void main() {
setlocale(0, "rus");
TicTacToe tictac;
tictac.records.readFrom();
tictac.mainmenu();
}

第四:

Records 类是完全错误的:它有一个 std::vector of Records*,这使它递归。
你应该有实例:

记录.h

class Record {
protected:
string player;
int wins;
int games;
int draws;
public:
Records();
Records(const std::string& player, const int& wins, const int& games,const int& draws);
~Records();
void setName(const std::string& player);
void setWins(const int& wins);
void setGames(const int& games);
void setDraws(const int& draws);
string getName() const;
int getWins() const;
int getGames() const;
int getDraws() const;
void addRec(const std::string& player, const int& wins, const int& games,const int& draws);
void sortStats();
void outRec();
void saveTo();
void readFrom();
};

记录.h

class Records : public std::vector<Record*> {
}

这更有意义,至少对我而言。

关于C++ 错误 1 ​​错误 C2146 : syntax error: missing ';' before identifier 'records' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759852/

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