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. Unexpected output qt c++ function
Forum Updated to NodeBB v4.3 + New Features

Unexpected output qt c++ function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 6.0
11 Posts 5 Posters 918 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.
  • AxelViennaA Offline
    AxelViennaA Offline
    AxelVienna
    wrote on last edited by
    #2

    A bit hard to guess since your code is pretty nested. You should definitively review your function as two issues stand out:

    • you define pre_index as static. Why? It will only be initialized on the first function call. Its value upon return will be used for all other calls. Does not seem like what you want and may trigger unexpected behavior. Also: Why do you use static in your first for loop?
    • your function will trigger an exception if you call it with the string “<“.

    Why don’t you set a break point inside on_pushButton_4_clicked() and debug your way through your function?

    C++ and Python walk into a bar. C++ reuses the first glass.

    Z 1 Reply Last reply
    2
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Folow-up from here: https://forum.qt.io/topic/132702/unresolved-external-symbol-class-std-basic-_string/
      As said before - learn to use a debugger.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • AxelViennaA AxelVienna

        A bit hard to guess since your code is pretty nested. You should definitively review your function as two issues stand out:

        • you define pre_index as static. Why? It will only be initialized on the first function call. Its value upon return will be used for all other calls. Does not seem like what you want and may trigger unexpected behavior. Also: Why do you use static in your first for loop?
        • your function will trigger an exception if you call it with the string “<“.

        Why don’t you set a break point inside on_pushButton_4_clicked() and debug your way through your function?

        Z Offline
        Z Offline
        Zeyad Khaled
        wrote on last edited by
        #4

        @AxelVienna i was not static but i got a warning from qt ro make it static so i just tried doing that , but it does'nt work either ways, thanks for helping

        JonBJ 1 Reply Last reply
        0
        • Z Zeyad Khaled

          @AxelVienna i was not static but i got a warning from qt ro make it static so i just tried doing that , but it does'nt work either ways, thanks for helping

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @Zeyad-Khaled
          Apart from what others have said about debug your code and/or put qDebug() statements into it if you want to know how it is behaving.

          What does your code actually do, and why in the world do you do it the way you do it? The code is opaque. Writing chunks of code to output bits like </, > and doing pops and checks and so on seems complex and error-prone. Why don't you use something like QDomDocument, or at least some node-structural-hierarchy of your own, to generate output from, instead of outputting piecemeal?

          Z 1 Reply Last reply
          0
          • JonBJ JonB

            @Zeyad-Khaled
            Apart from what others have said about debug your code and/or put qDebug() statements into it if you want to know how it is behaving.

            What does your code actually do, and why in the world do you do it the way you do it? The code is opaque. Writing chunks of code to output bits like </, > and doing pops and checks and so on seems complex and error-prone. Why don't you use something like QDomDocument, or at least some node-structural-hierarchy of your own, to generate output from, instead of outputting piecemeal?

            Z Offline
            Z Offline
            Zeyad Khaled
            wrote on last edited by
            #6

            @JonB it's A College Project and we are Required to write Code Using native c++ and i didn't write those functions my teammembers wrote it , so iam just trying to collect everything together and to check errors to make it just work , thank for helpinng

            JonBJ 1 Reply Last reply
            0
            • Z Zeyad Khaled

              @JonB it's A College Project and we are Required to write Code Using native c++ and i didn't write those functions my teammembers wrote it , so iam just trying to collect everything together and to check errors to make it just work , thank for helpinng

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Zeyad-Khaled
              Well it sure is an opaque piece of code they have written, goodness knows why it works in one environment but not another.

              I don't know what

              the function works well on Visual studio and the output is the desired one ,But when i try to embed it in my app it works but the output is not what i exactly was looking for

              means. vscode is just an IDE for writing/running code. Code does not behave one way when run within vscode and another way outside of it. One does not get "different output" under vscode compared to (what you call) "Qt Output". When you say that, what do you mean, do you mean you use a different compiler in Qt Creator versus in vscode?

              What does it actually do anyway? It seems to take some (potentially) malformed XML input and then make some of its own "guesses" about how to "correct" the input to produce some (well-formed?) XML output from it, is that it? I have no idea how it works/is supposed to work, as there is no one way to "guess"/"correct" any ill-formed XML input, there are a non-ending number of ways one might choose for what the malformed input might intend and so what to do to change it.

              Z 1 Reply Last reply
              0
              • JonBJ JonB

                @Zeyad-Khaled
                Well it sure is an opaque piece of code they have written, goodness knows why it works in one environment but not another.

                I don't know what

                the function works well on Visual studio and the output is the desired one ,But when i try to embed it in my app it works but the output is not what i exactly was looking for

                means. vscode is just an IDE for writing/running code. Code does not behave one way when run within vscode and another way outside of it. One does not get "different output" under vscode compared to (what you call) "Qt Output". When you say that, what do you mean, do you mean you use a different compiler in Qt Creator versus in vscode?

                What does it actually do anyway? It seems to take some (potentially) malformed XML input and then make some of its own "guesses" about how to "correct" the input to produce some (well-formed?) XML output from it, is that it? I have no idea how it works/is supposed to work, as there is no one way to "guess"/"correct" any ill-formed XML input, there are a non-ending number of ways one might choose for what the malformed input might intend and so what to do to change it.

                Z Offline
                Z Offline
                Zeyad Khaled
                wrote on last edited by
                #8

                @JonB i agree with in that point , i tried putting the input in a different way that he expects and it ofcourse pops an error and the program terminates , the code should take xml file nad preetify it but it doesn't work in alot of cases except the only case he has assumed , Shall i get a help about the structure to use or a way to write this in a good Structure

                1 Reply Last reply
                0
                • AxelViennaA Offline
                  AxelViennaA Offline
                  AxelVienna
                  wrote on last edited by
                  #9

                  You may want to test your code in Qt Creator with the same code as in vscode, i.e. with a hard coded string variable? Make the same call twice with the same input string, to rule out misbehavior due to the static variable. BTW, show us your compiler warnings that you eliminate by making i static in your for loop.

                  C++ and Python walk into a bar. C++ reuses the first glass.

                  1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #10

                    That static loop variable made me raise an eyebrow too. at function scope it would set the initial value on the first iteration and then all future reference would be based on whatever the last value it it was. OP doesn't state whether the function in which it is called is used once or called multiple times....but yeah, a static loop control var doesn't really make sense unless it servers as a global accumulator, in which case for() isn't really appropriate.

                    1 Reply Last reply
                    1
                    • JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      Making the for counter static will definitely make the behaviour go wrong if the function is called more than once. If it is the case that there are "compiler warnings that you eliminate by making i static in your for loop" I too would like to see the original warnings.

                      Separately there are incorrect assumptions about what you can afford to do to i in the loop. For example, in a loop which limits it to i < xml_edit.length() one cannot afford to go:

                          i++;
                          if (xml_edit[i] != '/')
                      

                      and other similar cases.

                      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