PDA

View Full Version : Need help with basic C++



arturs
5th April 2015, 17:47
I need help again.

I created a class "Count" there are some methods,argument etc.

In MainWidnow I created table of Objects
"
Count number[15];

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

}

From MainWidnow object I created new object preview

"
void MainWindow::on_actionActionpreview_triggered()
{

preview = new Preview(this);
preview->setModal(true);
preview->get_path(&filename);
preview->exec();

}"

I would like to see "number object" which I created in MainWindow from "preview". How to do it ?


I hope I described my problem clearly.

ChrisW67
5th April 2015, 22:08
Please use
... tags around code.

Where is the numbers[] array declared?
How have you tried to access it?
How has that failed?

Have you considered explicitly passing the number array to the preview object ?

arturs
5th April 2015, 22:51
I have the following Count class



count.h

class Count
{

private:

bool table[19][8];

QString line

public:

int start_line,end_line,alarm_lines; start_line,end_line,alarm_lines;
};

in MainWindow.cpp

I create new objects "Count number [15]" this way



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "count.h"

Count number[15];

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

ui->setupUi(this);

}



and in one of methods MainWindows I create new Object




void MainWindow::on_actionActionpreview_triggered()
{

preview = new Preview(this);
preview->setModal(true);
preview->exec();


}


In the method void Preview::on_pushButton_clicked() I would like to change start_line in object number[0]



#include "preview.h"
#include "ui_preview.h"
#include <QDebug>
#include "mainwindow.h"

Preview::Preview(QWidget *parent) :
QDialog(parent),
ui(new Ui::Preview)
{

ui->setupUi(this);


}


void Preview::on_pushButton_clicked()
{
number[0].start_line = 5;
}



but during compilation I have the following error :" 'alarm' was not declared in this scope"


What are the possibilities to change the start line for the number [0] ?

Regards
Artur

ChrisW67
6th April 2015, 00:21
The error message has nothing to do with your question.
Where is the code that tries to use a variable/name "alarm"? The error message from the compiler will have the file and line number.

jefftee
6th April 2015, 02:22
I create new objects "Count number [15]" this way



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "count.h"

Count number[15];

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

ui->setupUi(this);

}



Since you are allocating an array of your Count class outside of any class definition, you're are creating a global variable, which you should generally try to avoid doing. You should move that to make your Count array a member variable in your MainWindow class IMHO.

arturs
6th April 2015, 08:24
Sorry I made a mistake. I mean number not alarm and I would like to use it in the code



#include "preview.h"
#include "ui_preview.h"
#include <QDebug>
#include "mainwindow.h"

Preview::Preview(QWidget *parent) :
QDialog(parent),
ui(new Ui::Preview)
{

ui->setupUi(this);


}


void Preview::on_pushButton_clicked()
{
number[0].start_line = 5;
}

anda_skoa
6th April 2015, 08:46
The easiest way of having two classes share access to data is to pass a reference or pointer to that variable from the the object that created the variable to the other object.
In your case passing a reference or point to "number" as a constructor argument of "Preview".

Cheers,
_

arturs
6th April 2015, 08:48
Could you give me an example this method and other method which are possible ?