在 *.cpp中定义函数而不是在 *.h中定义函数时,未定义的参考错误

undefined-reference Error when defining a function in the *.cpp instead of in the *.h

本文关键字:函数 定义 未定义 参考 错误 cpp      更新时间:2023-10-16

我有一个问题:我有两个类: mainWindow ergebnisausfortran 看起来像这样:

mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H   
    #include <QMainWindow>
    #include <QDebug>
    #include <QString>
    #include "ErgbnisAusFortran.h"        
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();        
    public:
        ErgbnisAusFortran Berechnung();        
        ErgbnisAusFortran Berechnung_1()
        {
           ErgbnisAusFortran ret;
           qDebug() << " ich berechne Berechnung_1..." ;               
           return ret;
        }
    private slots:
        void on_pb_Calculate_clicked();      
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ErgbnisAusFortran.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pb_Calculate_clicked()
{
     ErgbnisAusFortran Ergebnis_1;
     ErgbnisAusFortran Ergebnis;
     Ergebnis_1 = Berechnung_1();
     Ergebnis = Berechnung();
}
ErgbnisAusFortran Berechnung()
{
    ErgbnisAusFortran ret;
    qDebug() << " ich berechne..." ;
    return ret;
}

困扰我的事情是:

我有2种方法Berechnung()和Berechnung_1()。

berechnung()是在mainwindow.h和在mainwindow.cpp

中定义

berechnung_1()是在mainwindow.h和在mainwindow.h

中定义

当我运行程序时,我会收到有关berechnung()的以下错误:

对mainwindow :: berechnung()的未定义引用。Berechnung_1效果很好。这让我感到困惑,因为我在mainwindow.cpp中包括mainwindow.h

有人知道怎么了吗?

谢谢

itelly

您忘了符合成员函数的名称:

ErgbnisAusFortran MainWindow::Berechnung()
                  ^^^^^^^^^^^^

因此,这是一个新的非会员函数,使成员函数不确定。