简体   繁体   中英

function cannot be overloaded error c++

I'm learning composition by making a connect 4 game that has a column class within a board class. I have spent forever trying to figure out these overloading errors:

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(){}
            ^

Here are my constructors:

#include <iostream>
using namespace std;

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

C4Board(){
  numCol = 7;
}

C4Board(int columns){
  numCol = columns;
}

~C4Board(){}

I got rid of the C4Board:: before the constructors because it gave me this error in addition to the overloading errors:

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

Here is my .h file, although I don't believe it's the problem:

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

Any help would be greatly appreciated.

compiler is recognizing functions as different, you have to specify that these are the definitions of your class methods...

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

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

If that really is your .h file, it's missing a final } . That can give these symptoms.

Remember that the #include can be seen as putting the entire text of the .h file into the .cpp file. A missing final } results in your function definitions from the .cpp file being inside the class definition from the .h file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM