Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member function redeclaration not allowed

If I define the function in mainwindow.cpp the function works, but when I define it in radiobuttons.cpp, and attempt to call it from mainwindow.cpp, the project won't compile.

mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H        
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

        public:
            explicit MainWindow(QWidget *parent = 0);

            void build_radios();  //this function

            ~MainWindow();

    };    
#endif // MAINWINDOW_H

radiobuttons.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

    void MainWindow::build_radios()
     {  

    //... some code

     }

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::radio_buttons();   //error: C2761: 'void MainWindow::build_radios(void)' : member function redeclaration not allowed

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

{

radio_buttons();

}
like image 941
chuckieDub Avatar asked Sep 23 '26 15:09

chuckieDub


2 Answers

That's not a definition, the compiler sees it as a declaration of a member function outside the class definition, which is illegal. Just remove that line. It shouldn't be there in the first place, it has no use.

In fact, move the actual definition from radiobuttons.cpp to mainwindow.cpp for consistency. Why declare a MainWindow member in a different implementation file?

like image 59
Luchian Grigore Avatar answered Sep 27 '26 17:09

Luchian Grigore


but when I define it in radiobuttons.cpp...

Except that you don't define but re-declare it. (And even that's redundant since you have the header file of the class included in that particular file.) The error message says pretty much what the problem is:

member function redeclaration not allowed

When you declare the MainWindow class, you have the void build_radios(); declaration inside - that in itself is enough for declaring the method. In the .cpp file, it's sufficient to implement it only, i. e. provide a definition only - you already have a declaration, coming from declaration of the class in the header file.

(In fact, you also have a definition, but that's in the radiobuttons.cpp file - it should be in the mainwindow.cpp. Fit all the methods of a class into one implementation file, don't speread them across multiple files and classes and... and... and...)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!