September 7, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

[Moved] Document/View application

Page  
1

I am working on an application where I need to pass a large structure or structure array between classes.

The application contains three classes: MainWindow, UserEntry and Calculations

The structure has two parts: user data and calculated results.

What I would like to do is: 1) use the MainWindow to create, open, save, etc. the structure, 2) use the UserDataEntry class to allow the user to input data to the structure, and 3) use the Calculations class to parse the user data, perform calculations and store the calculated results in the structure.

I need to be able to pass the structure (document) easily between the classes (views). Since the application is entirely self contained (no threading) making the structure global should work although I have been unable to get globals to work in Qt.

Instead of using a global structure I have been trying to get the following code to work for passing the structure between MainWindow and UserEntry:

MainWindow.h

#include “userentry.h”

public: struct information { … }info;

MainWindow.cpp … userdata(&info); …

UserEntry.h … void userdata(struct information *info); …

UserEntry.cpp

void UserEntry::userdata(struct information *i) { … }

This gives an error message:

no matching function for call to ‘UserEntry::userdata(MainWindow::information*)’
candidates are ‘void UserEntry::userdata(information*)’

I may have a simple syntax error, but I can’t find it or any reference to what it could be. I would appreciate any observations on the syntax and references to public applications (such as examples) that use this approach.

Thanks.

[Edit: Moved to C++ Gurus; mlong]

19 replies

September 7, 2011

mlong mlong
Mad Scientist
1517 posts

Why not define Information in its own header file, rather than defining it within MainWindow.h? MainWindow can still own the working copy of the structure, but it may make for a cleaner implementation. You can just include “information.h” wherever it might be needed.

Also, in C++ you don’t need to use the struct keyword when referring to the item in parameter lists, etc.. The struct name is sufficient. I.e., void UserEntry::userdata(Information *info) { }

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

September 7, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

Thanks for the quick reply.

Creating “information.h” seems like a good solution, but how can I make it work? How would MainWindow own the working copy and pass it to the other classes?

In Qt I kept getting compile errors when I didn’t include the struct keyword in places where it seemed redundant.

September 7, 2011

mlong mlong
Mad Scientist
1517 posts

Information.h:

  1. struct Information {
  2.  // foo
  3. };

UserEntry.h:

  1. #include "Information.h"
  2. ...
  3.    void userdata(Information *info) {...};  // Takes a pointer to info
  4.    // or
  5.    void userdata(Information &info) {...}; // Takes a reference to info

MainWindow.h:

  1. #include "Information.h"
  2. ...
  3.    private:
  4.       Information info;
  5.       UserEntry *userEntry;

MainWindow.cpp:

  1. MainWindow::someMethod() {
  2.    userEntry->userdata(&info);  // Pass the address of info.
  3.    // or
  4.    userEntry->userdata(info); // using reference
  5. }

Edit: Corrected code. Thanks Gerolf!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

September 7, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

Thanks, I’ll give it a try.

September 8, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi mlong,

I think you meant:

MainWindow.cpp:

  1. MainWindow::someMethod() {
  2.    userEntry->userdata(&info);  // Pass the address of info.
  3.    // or
  4.    userEntry->userdata(info); // using reference
  5. }

as userEntry is a pointer to the UserEntry class…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

Note that you might want to use forward declares in headers instead of includes. That means:

  1. //MainWindow.h
  2. struct Information;
  3.  
  4. //...
  5. private:
  6.    Information* m_information;

and then only in the MainWindow.cpp do the #include of Information.h.

It makes compilation faster.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 8, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

No luck – below is the MainWindow and Information code. I left out the UserData code to see if this would work before making it any more complex. This generates a compile error code:

request for member ‘a’ in ‘info’, which is of non-class type ‘Information*’

Is this caused by a syntax error that I am missing?

MainWindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include "Information.h"
  6.  
  7. namespace Ui {
  8.     class MainWindow;
  9. }
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     explicit MainWindow(QWidget *parent = 0);
  17.     ~MainWindow();
  18.  
  19. private:
  20.     struct information *info;
  21.  
  22. private:
  23.     Ui::MainWindow *ui;
  24. };
  25. #endif // MAINWINDOW_H

MainWindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4.  
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7.     QMainWindow(parent),
  8.     ui(new Ui::MainWindow)
  9. {
  10.  
  11.     ui->setupUi(this);
  12.  
  13.     info = new information;
  14.  
  15.     info.a = "A";
  16.     info.b = "B";
  17.     info.c = "C";
  18.  
  19.  
  20. }
  21.  
  22. MainWindow::~MainWindow()
  23. {
  24.     delete ui;
  25. }

Information.h

  1. #ifndef INFORMATION_H
  2. #define INFORMATION_H
  3.  
  4. #include <QtGui>
  5.  
  6. struct information
  7. {
  8.     QString a;
  9.     QString b;
  10.     QString c;
  11. };
  12.  
  13. #endif // INFORMATION_H

Added @ tags around code sections. Please do that yourself next time; Andre

September 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

There seems to be some weird mismatch of #ifdefs and #endif’s in your code. Perhaps you should fix these first?

Oh, and in your MainWindow, you have a pointer to an Information instance, while you do not seem to be creating an instance and trying to assign to it as if it is a normal member variable.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 8, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

That seems to have come from copying and pasting. The definitions are okay in my files.

September 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

CapnMikey wrote:
That seems to have come from copying and pasting. The definitions are okay in my files.

Then please update your previous post to reflect the actual state of your code.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 8, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

Line 15 in Information.h should be line 25 in MainWindow.h. Sorry about the mixup.

September 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

CapnMikey wrote:
Line 15 in Information.h should be line 25 in MainWindow.h. Sorry about the mixup.

Please just use the Edit button next to your post to modify it and fix the mixup.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 8, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

Okay, the code above has been modified.

Thanks for pointing out the edit button.

September 8, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Your struct is named information, but the definition in mainwindow.h is Information. C++ is case sensitive…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 8, 2011

CapnMikey CapnMikey
Lab Rat
10 posts

I corrected the case error as well (see post above). I still get the same error message.

Page  
1

  ‹‹ [Moved, Solved] problems with "const"      [Split] Set library version in DLL with VS 2010 ››

You must log in to post a reply. Not a member yet? Register here!