嗨,我无法编译我的程序,由于某种原因,它给出了有关命名空间的错误,不知道为什么,请帮忙?错误是:
"expected unqualified-id before 'namespace' filminput.h 11
UI has not been declared filminput.h 26
ISO C++ forbids declaration of 'FilmInput' with no type filminput.h 26
expected ';' before '*' token filminput.h 26
这是代码:
#ifndef FILM_H
#define FILM_H
#include <QWidget>
#include <QString>
#include <QDate>
class Film : public QWidget{
public:
Film(QString t,int dur,QString dir,QDate r);
Film();
void setTitle(QString t);
void setDuration(int dur);
void setDirector(QString dir);
void setReleaseDate(QDate r);
QString getTitle() const;
int getDuration() const;
QString getDirector() const;
QDate getReleaseDate() const;
private:
QString m_title;
int m_duration;
QString m_director;
QDate m_releaseDate;
};
#endif // FILM_H
#ifndef FILMWRITER_H
#define FILMWRITER_H
#include "Film.h"
#include <QtGui>
class FilmWriter {
public:
void saveFilm(Film& f);
}
#endif // FILMWRITER_H
#ifndef FILMINPUT_H
#define FILMINPUT_H
#include <QMainWindow>
#include "Film.h"
#include "FilmWriter.h"
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
namespace Ui {
class FilmInput;
}
class FilmInput : public QMainWindow
{
Q_OBJECT
public:
explicit FilmInput(QWidget *parent = 0);
~FilmInput();
void obtainFilmData(Film& f);
public slots:
void getFilm();
private:
Ui::FilmInput *ui;
//widgets
QMainWindow* window;
QLabel* infoLabel;
QLabel* titleLabel;
QLabel* durationLabel;
QLabel* directorLabel;
QLabel* relDateLabel;
QTextEdit* titleEdit;
QTextEdit* durationEdit;
QTextEdit* directorEdit;
QTextEdit* relDateEdit;
QPushButton* saveBtn;
QPushButton* cancelBtn;
Film f;
//sets up gui and connects signals and slots
void setUpGui();
};
#endif // FILMINPUT_H
#include "Film.h"
Film::Film(QString t,int dur,QString dir,QDate r):m_title(t),m_duration(dur),m_director(dir),m_releaseDate(r){
}
Film::Film(){
}
void Film::setTitle(QString t){
m_title = t;
}
void Film::setDuration(int dur){
m_duration = dur;
}
void Film::setDirector(QString dir){
m_director = dir;
}
void Film::setReleaseDate(QDate r){
m_releaseDate = r;
}
QString Film::getTitle() const{
return QString("%1").arg(m_title);
}
int Film::getDuration() const{
return m_duration;
}
QString Film::getDirector() const{
return QString("%1").arg(m_director);
}
QDate Film::getReleaseDate() const{
return m_releaseDate;
}
#include "FilmWriter.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
void FilmWriter::saveFilm(Film& f){
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"));
if (fileName != "") {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));// error message
} else {
QTextStream stream(&file);
stream << f->toPlainText();
stream.flush();
file.close();
}
}
}
#include "filminput.h"
#include "ui_filminput.h"
#include <QtGui>
#include "Film.h"
#include "FilmWriter.h"
#include <QTextEdit>
FilmInput::FilmInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FilmInput)
{
ui->setupUi(this);
setUpGui();
}
FilmInput::~FilmInput()
{
delete ui;
}
void FilmInput::setUpGui(){
//initialise widgets
infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
titleLabel = new QLabel("Film Title",this);
durationLabel = new QLabel("Film Duration",this);
directorLabel = new QLabel("Film Director",this);
relDateLabel = new QLabel("Film Release Date",this);
titleEdit = new QTextEdit(this);
durationEdit = new QTextEdit(this);
directorEdit = new QTextEdit(this);
relDateEdit = new QTextEdit(this);
saveBtn = new QPushButton("Save Film",this);
cancelBtn = new QPushButton("Cancel",this);
//set layout
QFormLayout* layout = new QFormLayout();
layout->addWidget(infoLabel);
layout->addWidget(titleLabel);
layout->addWidget(titleEdit);
layout->addWidget(durationLabel);
layout->addWidget(durationEdit);
layout->addWidget(directorLabel);
layout->addWidget(directorEdit);
layout->addWidget(relDateLabel);
layout->addWidget(relDateEdit);
layout->addWidget(saveBtn);
layout->addWidget(cancelBtn);
this->ui->widget->setLayout(layout);
this->setWindowTitle("Film Archive");
connect(saveBtn,SIGNAL(clicked()),this, SLOT(getFilm()));
connect(cancelBtn,SIGNAL(clicked()),this,SLOT(close()));
}
void FilmInput::getFilm(){
Film f1;
obtainFilmData(f1);
}
void FilmInput::obtainFilmData(Film &f){
f(titleEdit->toPlainText(),durationEdit->toPlainText().toInt() ,directorEdit->toPlainText(),
QDate::fromString(relDateEdit->toPlainText(),"dd/MM/YYYY"));
saveFilm(f);
}
#include <QtGui/QApplication>
#include "filminput.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FilmInput w;
w.show();
return a.exec();
}