Qt error: symbol(s) not found for architecture x86_64 on Mac OS X Yosemite

Qt error: symbol(s) not found for architecture x86_64 on Mac OS X Yosemite

在 mac OS X 10.10 和 Xcode 6.1.1 上使用 qt5.4 编写程序,在尝试编译时出现以下错误:

error: symbol(s) not found for architecture x86_64

编译器输出显示:

Undefined symbols for architecture x86_64:
  "data::SingleLineData", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Integral.app/Contents/MacOS/Integral] Error 1
22:52:25: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Integral (kit: Desktop Qt 5.4.0 clang 64bit) 
When executing step "Make"

这是我的 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class data{
public:
    static QVector<double> SingleLineData;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT


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

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

还有我的mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QtCore>
#include <QTextBrowser>
#include <QCheckBox>
#include <QDebug>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{


    data::SingleLineData.resize(512);
    QString test;
    QString inputfile = QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "/Users",
                "All files (*.*)"
                );

    if(inputfile != ""){
    QFile file(inputfile);


    if(!file.open(QFile::ReadOnly)){
       }
    QTextStream in(&file);


        double buffer;

        while(!file.atEnd()){
            in.readLine();
            for(int i=0; i<512; i++){
                in >> buffer;
                data::SingleLineData[i]+=buffer;
            }
        }

    }
        qDebug() << data::SingleLineData;
// ************* file output **************************************************

        QString filename = "/Users/Mitch/Desktop/integral.txt";
        QFile fileout(filename);
        if (fileout.open(QFile::ReadWrite | QFile::Truncate)){
         QTextStream out(&fileout);
         for (QVector<double>::iterator iter = data::SingleLineData.begin(); iter != data::SingleLineData.end(); iter++){
             out << *iter <<", ";
         }
         fileout.close();
        }
}


void MainWindow::on_pushButton_2_clicked()
{
    QString inputfile = QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "/Users",
                "All files (*.*)"
                );
    QVector<double> SingleChannel;
    if (inputfile != ""){
        QFile file(inputfile);

        if (!file.open(QIODevice::ReadOnly)){
        }
        QTextStream in(&file);
        SingleChannel.resize(1);
        double buffer;
        int channelnumber = ui->spinBox->value();
        while(!file.atEnd()){
            in.readLine();
            for (int i = 0; i < 512; i++){
                in >> buffer;
                if (i == channelnumber){
                    SingleChannel.push_back(buffer);
                }
                data::SingleLineData[i]+=buffer;
            }
        }
    }
    qDebug() << SingleChannel;
}

我认为当头文件中声明了一个函数但未在 .cpp 文件中实现时会发生错误。据我所知,这两个按钮都是在 .cpp 文件中实现的,这就是编译器所抱怨的。

有什么想法吗? 谢谢 米奇

编译器抱怨找不到 其中 data::SingleLineData 被定义。它知道它已被声明,但由于它是静态成员,因此必须在文件范围内定义它,以便它的内存存在于某处并可以链接到。 see here.

QVector<double> data::SingleLineData; 添加到您的 .cpp 文件以定义静态成员。