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] Pass result "y" to inline function
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Pass result "y" to inline function

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.1k Views 2 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.
  • N Offline
    N Offline
    newe12
    wrote on last edited by newe12
    #1

    Hi!

    I have to pass the result "y", which is correctly calculated in a function in mainwindow.cpp, to the inline function in the header file (where "y" is needed):

    inline double f(double x)
    {
          double y;
          return y+3*x;
    }
    

    this function is called in the same function of the mainwindow.cpp-file where "y" is calculated before.

    It compiles fine, but "y" is not passed.

    How can I tell the programme to pass "y" to be used in the inline function of the header?

    I appreciate any useful comments!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      That's rather a general C++ question but:

      Is y a member of your class ? If not, add it as parameter to f otherwise, remove double y;

      On a side note, you should consider using more meaning full names for your functions and variables. Your code will be way easier to read and understand.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • N Offline
        N Offline
        newe12
        wrote on last edited by newe12
        #3

        Thanks @SGaist ! Solved!

        I changed it to:

        inline double fun(double x, double constant)
        {
        return constant+3*x;
        }

        and it works!

        1 Reply Last reply
        0
        • sosunS Offline
          sosunS Offline
          sosun
          wrote on last edited by sosun
          #4

          I can't get the point, but I'll try: You want the y to be passed to the fun with its calculated before value? Am I right? If so, you probably want to pass it by reference:

          inline double fun(double x, double& constant) {
              return constant+3*x;
          }
          

          Also, you should not call variables "constant" until they're constant like const double sum; or whatever with const keyword.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @sosun, why as a reference ? constant is just used not modified.

            But I agree on the naming point. By the way, a constant that is recalculated is not really a constant

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            sosunS 1 Reply Last reply
            1
            • SGaistS SGaist

              @sosun, why as a reference ? constant is just used not modified.

              But I agree on the naming point. By the way, a constant that is recalculated is not really a constant

              sosunS Offline
              sosunS Offline
              sosun
              wrote on last edited by
              #6

              @SGaist Aah, I didn't understand the question rightly. You was right.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                newe12
                wrote on last edited by newe12
                #7

                @SGaist that's true!

                "x" is the variable or better "argument" of the function "fun". The constant is basically the result of a variable from a function from within an interval. Nevertheless, it is calculated before "fun" is called and it is simply not changed any more within the function! So I renamed it to "resultbefore" :-).

                inline double fun(double x, double resultbefore)
                {
                return resultbefore+3*x;
                }
                

                Anyway, it works! There was a another small problem which I have removed now. Also thanks to @sosun !

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Again, it's not really better. What does that variable represent ? Number of atoms in a quartz crystal ? Number of waves hitting a riff ? That's the kind of thing you should refer to when deciding variable names.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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