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();
}
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.cpptomainwindow.cppfor consistency. Why declare aMainWindowmember in a different implementation file?