PDA

View Full Version : function definition



davidrhcp
7th May 2014, 12:33
i have a function within a function but i cant depict which part of my code is the function? also whats the best way to fix the issue? moving the functio into main.cpp or moving it to the top of the mainwindow.cpp

main.cpp

@


#include "mainwindow.h"
#include <QApplication>
#include <cstdlib>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <mainwindow.h>
#include <QFile>
#include <QString>
#include <QIODevice>
#include <QtGui>


// creating directory upon run for user input
QDir mDir;



QString mFile = ("C:/Monice/BW/MBW.txt");

int main(int argc, char *argv[])
{

QApplication a(argc, argv);
MainWindow w;
w.show();

mDir.mkpath("C:/Monice/BW");

return a.exec();
}

@


mainwindow.cpp

@


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QObject"
#include "QtCore"
#include "QNetworkConfigurationManager"
#include "QNetworkInterface"
#include "QHostAddress"
#include "QString"
#include "QFile"
#include "QLineEdit"
#include "QDir"
#include "QApplication"
#include "QMainWindow"

MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)



{
ui->setupUi(this);


//display network interfaces
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();

// each interface is allocated one after the other in a list
foreach (QNetworkInterface mon, list)

//adding the list to be shown in the interface combobox
{
ui->comboBox_Interface->addItem(mon.name());
}

// a for loop that counts from 50-90 in multiples of 5
for (int i = 50; i <= 90; i += 5)
//selecting the combox alert to diplay the figures 50-90 with a % symbol
{
ui->comboBox_Alert->addItem(QString::number(i) + "%");
}

// creating directory upon run for user input




void MainWindow::on_save_clicked()
{

QDir mDir;
mDir.mkpath("C:/Monice/BW");
QString MBW = ("C:/Monice/BW/MBW.txt");
QFile mFile(MBW);


if (mFile.open(QIODevice::WriteOnly))
{
QTextStream out(&mFile);
out << ui->lineEdit->text();
}



}

}




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

@

The error occurs in mainwindow.cpp saying that a function-definition cant appear before { token.
just after the void MainWindow::on_save_clicked() line. and the "expected at end of input }" error appears at the last line of the mainwindow.cpp

anda_skoa
7th May 2014, 15:26
Move the definition of void MainWindow::on_save_clicked() { ... } out of the scope of the constructor.

As mentioned in the other thread, learning the basics of C++, such as scopes, will help a lot when programming in C++.

Cheers,
_