在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文件中实现的,这就是编译器所抱怨的。
有什么想法吗? 谢谢 米奇
答案 0 :(得分:1)
编译器抱怨无法找到
其中data::SingleLineData
的定义。它知道它已被声明,但由于它是一个静态成员,它必须在文件范围内定义,以便它的内存存在于某处并且可以链接到。 see here
将QVector<double> data::SingleLineData;
添加到.cpp文件中以定义静态成员。