PDA

View Full Version : Public routines



eltecprogetti
5th April 2012, 08:47
I have a routine in my MainWindow, and I declared public this routine:


In MainWindow.cpp


void MainWindow::Read_Ambiente()
{
}



In MainWindow.h


public:
void Read_Ambiente();



In another form I want to use this routine:

In Diagnostica.cpp


#include "mainwindow.h"
extern void Read_Ambiente();


If I don't use this routine "Read_Ambiente" in the second form, there are no errors.

When I use the routine, I have this error from the linker:

diagnostica.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl Read_Ambiente(void)" (?Read_Ambiente@@YAXXZ) referenced in function "private: virtual bool __thiscall Diagnostica::eventFilter(class QObject *,class QEvent *)" (?eventFilter@Diagnostica@@EAE_NPAVQObject@@PAVQEv ent@@@Z)

mvuori
5th April 2012, 09:24
There is no extern void Read_Ambiente(). There is only void MainWindow::Read_Ambiente().
You need to make your MainWindow based object visible to other classes (preferebly as a constructor argument SomeClass::SomeClass(MainWindow *mainWindow)) and call its method.

mentalmushroom
5th April 2012, 09:34
// mainwindow.h
class MainWindow: public QMainWindow
{
public:
void readAmbiente();
};

// mainwindow.cpp
void MainWindow::readAmbiente()
{
// ...
}


// diagnostics.cpp
mainWindow->readAmbiente(); // mainWindow is the instance of MainWindow

eltecprogetti
5th April 2012, 13:58
I have already tried it as written by "mentalmushroom" but the linker does not recognize Read_Ambiente() like MainWindow member.

I must probably try like written by "mvuori", but I am not able to create a constructor in the MainWindow class and so finally call the class method.

Can someone explain in detail what should I do?

Thanks.

mentalmushroom
5th April 2012, 14:00
At the very first show how did you do this.

wysota
5th April 2012, 14:24
I have already tried it as written by "mentalmushroom" but the linker does not recognize Read_Ambiente() like MainWindow member.
Does it not recognize the member function or the object you're calling the method on? And the linker or the compiler? If the linker breaks the build process then most likely you didn't implement the method body (check that the signature of the implementation matches exactly the one in the declaration).

eltecprogetti
5th April 2012, 16:23
I was wrong, the error is given by the compiler.

When I write the call, the QT automatically recognizes the MainWindow routine, so that is writed by pressing enter.
But when the program is compiled, it report a syntax error: missing ';' before '.'

If I write "MainWindow.Read_Ambiente ();" or "MainWindow->Read_Ambiente ();" is equal, the error is the same.

mentalmushroom
5th April 2012, 16:29
Maybe you forgot to include mainwindow.h to the source file where you try to call its method. Show the code if you want to get a specific answer.

eltecprogetti
5th April 2012, 16:50
I resolved.

it was necessary to create a istance of MainWindow in the Diagnostics.cpp (Mainwindow *Main).

At this point I need to write Main->Read_Ambiente();

mentalmushroom
5th April 2012, 16:52
of course, it is necessary, hard to imagine how could you use it without having an instance :)

Spitfire
9th April 2012, 13:26
In some cases you could use 'static' method to call it wthout instance of an object.