函数不能重载错误c ++

时间:2017-02-01 06:36:09

标签: c++

我正在通过制作一个在棋盘类中具有列类的连接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];
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

如果这确实是您的.h文件,则会遗漏最后的}。这可能会产生这些症状。

请记住,#include可以看作是将.h文件的整个文本放入.cpp文件中。缺少最终}会导致.cpp文件中的函数定义 .h文件中的类定义。