[SOLVED] how to pass variable and call function in other cpp
-
Hello i got a problem that takes me for days ..
i already tried to solve the problem using google but no success.i got 3 files
main.cpp
main_window.cpp
main_window.hi try to pass the variable 'teststring' from main.cpp to main_window.cpp and call the function 'ausgabe()' in main_window.cpp
but i always get this error:
no member named 'ausgabe' in 'main_window'can someone probably help me ??
sure i want to use it on a gui, but for solving this problem i tried in in the console just to only face this problem ..
thanks for any advice,
O.- main_window.h:
@
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
#include <QCoreApplication>
#include <QDebug>class main_window
{
public:
main_window();
};#endif // MAIN_WINDOW_H
@- main.cpp:
@
#include "main_window.h"
extern QString teststring;
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
//begin ->
QString teststring = "This is a teststring.";main_window testaufruf; testaufruf.ausgabe(teststring);
//<- end
return a.exec();
}
@- main_window.cpp:
@
#include "main_window.h"
QString teststring;
void ausgabe(QString &teststring){
qDebug() << teststring;
}main_window::main_window()
{
}
@
[andreyc EDIT]: Added @ around code.
- main_window.h:
-
Please use code tags (first button on the right) when pasting code.
Your ausgabe function is only defined in the main_window.cpp and as such - invisible to the rest of the program. To make it visible decalre it in the main_window.h file:
@
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
#include <QCoreApplication>
#include <QDebug>void ausgabe(QString &teststring);
//...
#endif // MAIN_WINDOW_H
@
After that you can call it like that in main.cpp:
@
ausgabe(teststring);
@
ausgabe is a free function, not a member of class testaufruf so you don't call it like you did: testaufruf.ausgabe(...).Btw. extern and global variables are really ugly and should be avoided for such simple cases. I would suggest to grab a c++ basics book and learn a proper oo and design.
Also if you pass a string to a function that just reads it (prints to debug in this case) make it a const QString&. non-const refs are used for output parameters. -
oh - first thanks for the hint with the code-tags
and 2nd - thank you soooo much for this advice - tried it before - but maybe made something wrong - now it works
you are great - !!!