Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] how to pass variable and call function in other cpp
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] how to pass variable and call function in other cpp

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 831 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    babuschkainyouface
    wrote on last edited by
    #1

    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.h

    i 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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        babuschkainyouface
        wrote on last edited by
        #3

        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 - !!!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved