gpt4 book ai didi

c++ - 函数不能重载错误c++

转载 作者:行者123 更新时间:2023-11-28 05:18:51 25 4
gpt4 key购买 nike

我正在通过制作一个在棋盘类中有一个列类的 connect 4 游戏来学习作文。我一直在努力找出这些重载错误:

g++ -g -c C4Board.cpp -o C4Board.o
C4Board.cpp:11:1: error: ‘C4Board::C4Board()’ cannot be overloaded
C4Board(){
^
In file included from C4Board.cpp:9:0:
C4Board.h:7:5: error: with ‘C4Board::C4Board()’
C4Board();
^
C4Board.cpp:20:1: error: ‘C4Board::~C4Board()’ cannot be overloaded
~C4Board(){}
^
In file included from C4Board.cpp:9:0:
C4Board.h:9:5: error: with ‘C4Board::~C4Board()’
~C4Board();
^
C4Board.cpp:20:12: error: expected ‘}’ at end of input
~C4Board(){}
^

这是我的构造函数:

#include <iostream>
using namespace std;

#include "C4Col.h"
#include "C4Board.h"

C4Board(){
numCol = 7;
}

C4Board(int columns){
numCol = columns;
}

~C4Board(){}

我在构造函数之前去掉了 C4Board::,因为除了重载错误之外,它还给我这个错误:

    error: extra qualification ‘C4Board::’ on member ‘C4Board’ [-fpermissive]
C4Board::C4Board(){
^

这是我的 .h 文件,但我不认为这是问题所在:

class C4Board{
public:
C4Board();
C4Board(int);
~C4Board();
void display();
void play();
private:
int numCol;
C4Col Board[7];
}

如有任何帮助,我们将不胜感激。

最佳答案

编译器将函数识别为不同的,您必须指定这些是类方法的定义...

C4Board::C4Board(){
numCol = 7;
}

C4Board::C4Board(int columns){
numCol = columns;
}
C4Board::~C4Board(){}

关于c++ - 函数不能重载错误c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973368/

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