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. How to call function from the original class

How to call function from the original class

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 844 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello, I have a Qt Designer Form class. Let's call it A. It has A.cpp and A.h , and of course a class named A.
    For some reasons I created another class inside A.cpp, call it class B. I made a global object b from class B.

    Functions inside class A can easily call class B functions by b.somefunction();
    but when a class B function wants to call a class A function it can't.
    an error message said: call to non-static member function without an object argument.

    I guess there must be an object of class A or everything will make no sense, but what is it?

    jsulmJ 1 Reply Last reply
    0
    • M MasterBlade

      Hello, I have a Qt Designer Form class. Let's call it A. It has A.cpp and A.h , and of course a class named A.
      For some reasons I created another class inside A.cpp, call it class B. I made a global object b from class B.

      Functions inside class A can easily call class B functions by b.somefunction();
      but when a class B function wants to call a class A function it can't.
      an error message said: call to non-static member function without an object argument.

      I guess there must be an object of class A or everything will make no sense, but what is it?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MasterBlade said in How to call function from the original class:

      I guess there must be an object of class A or everything will make no sense, but what is it?

      Yes, you need an instance (object) of a class to call its non static methods:

      A a;
      a.doSomething();
      

      a is here the instance (object).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @MasterBlade said in How to call function from the original class:

        I guess there must be an object of class A or everything will make no sense, but what is it?

        Yes, you need an instance (object) of a class to call its non static methods:

        A a;
        a.doSomething();
        

        a is here the instance (object).

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @jsulm said in How to call function from the original class:

        @MasterBlade said in How to call function from the original class:

        I guess there must be an object of class A or everything will make no sense, but what is it?

        Yes, you need an instance (object) of a class to call its non static methods:

        A a;
        a.doSomething();
        

        a is here the instance (object).

        Actually I tried that and it worked. But isn't there a default instance when running the program? Or how can the window appear?

        B jsulmJ 2 Replies Last reply
        0
        • M MasterBlade

          @jsulm said in How to call function from the original class:

          @MasterBlade said in How to call function from the original class:

          I guess there must be an object of class A or everything will make no sense, but what is it?

          Yes, you need an instance (object) of a class to call its non static methods:

          A a;
          a.doSomething();
          

          a is here the instance (object).

          Actually I tried that and it worked. But isn't there a default instance when running the program? Or how can the window appear?

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @MasterBlade
          Yes, there may already be a instance.
          But you need a way to get access to it, like a pointer or something.
          You cannot just call a non-static member function of nothing.

          1 Reply Last reply
          -1
          • M MasterBlade

            @jsulm said in How to call function from the original class:

            @MasterBlade said in How to call function from the original class:

            I guess there must be an object of class A or everything will make no sense, but what is it?

            Yes, you need an instance (object) of a class to call its non static methods:

            A a;
            a.doSomething();
            

            a is here the instance (object).

            Actually I tried that and it worked. But isn't there a default instance when running the program? Or how can the window appear?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @MasterBlade said in How to call function from the original class:

            But isn't there a default instance when running the program?

            No, why should there be a default instance? It's your responsibility as developer to create one when needed.
            If you're talking about windows then take a look at main.cpp - you will see how main window is created there. Maybe you should explain what exactly you're trying to do?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            • Login

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