Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to run a C++ code in Qt?
QtWS25 Last Chance

How to run a C++ code in Qt?

Scheduled Pinned Locked Moved Installation and Deployment
123 Posts 5 Posters 108.2k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #104

    No, include them in your O_PS* files where suited.

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

    RelaR 1 Reply Last reply
    0
    • SGaistS SGaist

      No, include them in your O_PS* files where suited.

      RelaR Offline
      RelaR Offline
      Rela
      wrote on last edited by Rela
      #105

      @SGaist
      I copied the "O_PSA .H" and "O_PS .CPP" code in the "mainwindow.h" and "main.cpp" of the Qt project respevtively. I did add ```

      #include <stdio.h> 
      

      in either "mainwindow.h" or "main.cpp" files, but it gave the same errors

      kshegunovK 1 Reply Last reply
      0
      • RelaR Rela

        @SGaist
        I copied the "O_PSA .H" and "O_PS .CPP" code in the "mainwindow.h" and "main.cpp" of the Qt project respevtively. I did add ```

        #include <stdio.h> 
        

        in either "mainwindow.h" or "main.cpp" files, but it gave the same errors

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #106

        @Rela
        It must have taken you ages to delete those names ... Anyway, to use ostream, istream and other streams from the standard C++ library, you actually need to include the corresponding headers - stdio.h simply won't cut it. You need (depending on which classes you use) one of the *stream headers, e.g. #include <iostream>, #include <fstream> and so on. Additionally, these classes come in the std namespace so you'd need to expand that as well, for example.

        #include <iostream>
        
        using namespace std;
        
        // Only now the istream and ostream classes are available.
        

        Kind regards.

        Read and abide by the Qt Code of Conduct

        RelaR 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @Rela
          It must have taken you ages to delete those names ... Anyway, to use ostream, istream and other streams from the standard C++ library, you actually need to include the corresponding headers - stdio.h simply won't cut it. You need (depending on which classes you use) one of the *stream headers, e.g. #include <iostream>, #include <fstream> and so on. Additionally, these classes come in the std namespace so you'd need to expand that as well, for example.

          #include <iostream>
          
          using namespace std;
          
          // Only now the istream and ostream classes are available.
          

          Kind regards.

          RelaR Offline
          RelaR Offline
          Rela
          wrote on last edited by Rela
          #107

          @kshegunov
          There are

          using namespace std;
          #include <iostream>
          #include <fstream>
          #include <ostream>
          

          in the "main.cpp" already. I added ```

          #include <stdio.h>
          

          in each file, but it gave the same error.

          kshegunovK 1 Reply Last reply
          0
          • RelaR Rela

            @kshegunov
            There are

            using namespace std;
            #include <iostream>
            #include <fstream>
            #include <ostream>
            

            in the "main.cpp" already. I added ```

            #include <stdio.h>
            

            in each file, but it gave the same error.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #108

            @Rela said:

            There are ... in the "main.cpp" already.

            But obviously these headers are not present in mainwindow.h, whence the compile errors. The compiler doesn't know heck about ostream when processing the mainwindow.h and it's whining ...

            Kind regards.

            Read and abide by the Qt Code of Conduct

            RelaR 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @Rela said:

              There are ... in the "main.cpp" already.

              But obviously these headers are not present in mainwindow.h, whence the compile errors. The compiler doesn't know heck about ostream when processing the mainwindow.h and it's whining ...

              Kind regards.

              RelaR Offline
              RelaR Offline
              Rela
              wrote on last edited by
              #109

              @kshegunov
              Thanks. Do you know what else I can do except adding ```

              #include <stdio.h>
              

              to the "mainwindow.h"?

              kshegunovK 1 Reply Last reply
              0
              • RelaR Rela

                @kshegunov
                Thanks. Do you know what else I can do except adding ```

                #include <stdio.h>
                

                to the "mainwindow.h"?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #110

                @Rela

                Add the aforementioned headers to the mainwindow.h:

                #include <iostream>
                #include <fstream>
                #include <ostream>
                
                using namespace std;
                

                Otherwise the compiler will not know what ostream is.

                Read and abide by the Qt Code of Conduct

                RelaR 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @Rela

                  Add the aforementioned headers to the mainwindow.h:

                  #include <iostream>
                  #include <fstream>
                  #include <ostream>
                  
                  using namespace std;
                  

                  Otherwise the compiler will not know what ostream is.

                  RelaR Offline
                  RelaR Offline
                  Rela
                  wrote on last edited by
                  #111

                  @kshegunov
                  Done, but here are the errors cleaned again ;)

                  kshegunovK 1 Reply Last reply
                  0
                  • RelaR Rela

                    @kshegunov
                    Done, but here are the errors cleaned again ;)

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #112

                    @Rela
                    I'm sorry but I have no idea what's happening, with the deletions and without a complete code snippet this is just turning into a guessing game. Not to mention the preprocessor directives, whose expansions I have no way of deducing. The errors have obviously changed and they are at compile-time, so focus on that and try to resolve the type mismatches.

                    Read and abide by the Qt Code of Conduct

                    RelaR 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @Rela
                      I'm sorry but I have no idea what's happening, with the deletions and without a complete code snippet this is just turning into a guessing game. Not to mention the preprocessor directives, whose expansions I have no way of deducing. The errors have obviously changed and they are at compile-time, so focus on that and try to resolve the type mismatches.

                      RelaR Offline
                      RelaR Offline
                      Rela
                      wrote on last edited by
                      #113

                      @kshegunov
                      The problem is not related to the code. The C++ code works properly in MV and also it worked in Qt creator before and gave the results in "Application Outputs" with Qt5.5 and MV 2013. The problem happened, when I start working with MV 2015 and Qt5.6 beta.
                      I just did the previous way and added all C++ files to the Qt project files. Do you have any idea about the ```

                      error: dependent '..\C++' does not exist.
                      
                      kshegunovK 1 Reply Last reply
                      0
                      • RelaR Rela

                        @kshegunov
                        The problem is not related to the code. The C++ code works properly in MV and also it worked in Qt creator before and gave the results in "Application Outputs" with Qt5.5 and MV 2013. The problem happened, when I start working with MV 2015 and Qt5.6 beta.
                        I just did the previous way and added all C++ files to the Qt project files. Do you have any idea about the ```

                        error: dependent '..\C++' does not exist.
                        
                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #114

                        @Rela
                        I have never seen that error, but if I had to guess probably a folder can't be found by qmake. Inspect your project file, and make sure all folders/files exist. Then rerun qmake and then do a full rebuild. Additionally, don't use backslashes for your paths when working with qmake, use *nix-style paths, for example: C:/somefolder/somefile.cpp

                        Read and abide by the Qt Code of Conduct

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

                          Do you have something containing "C++" anywhere in your .pro file ?

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

                          RelaR 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Do you have something containing "C++" anywhere in your .pro file ?

                            RelaR Offline
                            RelaR Offline
                            Rela
                            wrote on last edited by
                            #116

                            @SGaist
                            Yes, I had something like this in PS8, because I added the C++ project header and source files through the "../C++ projects/po... /" path. But, now I removed the space of "C++ projects" (because maybe it makes problem), and changed the path withoutspace. PS10 Qt project .pro file is this
                            Now the error is this

                            1 Reply Last reply
                            0
                            • jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #117

                              What is char.h?

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

                              RelaR 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                What is char.h?

                                RelaR Offline
                                RelaR Offline
                                Rela
                                wrote on last edited by Rela
                                #118

                                @jsulm
                                It was

                                #include <tchar.h>
                                

                                in the C++ code, but it had given the errors like

                                'void PS... (char *)': cannot convert argument 1 from 'const char [37]' to 'char *'
                                

                                and I changed it to "char".
                                I think its related to the main function in "main.cpp"

                                int main(int argc, char* argv[])
                                {
                                
                                kshegunovK 1 Reply Last reply
                                0
                                • jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #119

                                  How is it related to main?
                                  char is a native data type in C and C++, you do not have to include any header file to use it. So just remove this include.
                                  This error:

                                  'void PS... (char *)': cannot convert argument 1 from 'const char [37]' to 'char *'
                                  

                                  You got probably because you're passing a const char string as parameter to a function which expects a non const char string:

                                  void PS... (char *param);
                                  then you call
                                  PS...("a string");
                                  

                                  Do it like this:

                                  char *param = "a string";
                                  PS...(param);
                                  

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

                                  1 Reply Last reply
                                  0
                                  • RelaR Rela

                                    @jsulm
                                    It was

                                    #include <tchar.h>
                                    

                                    in the C++ code, but it had given the errors like

                                    'void PS... (char *)': cannot convert argument 1 from 'const char [37]' to 'char *'
                                    

                                    and I changed it to "char".
                                    I think its related to the main function in "main.cpp"

                                    int main(int argc, char* argv[])
                                    {
                                    
                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by
                                    #120

                                    @Rela

                                    'void PS... (char *)': cannot convert argument 1 from 'const char [37]' to 'char *'

                                    The error is because you're passing const char * const to a function expecting char * and the compiler doesn't know how to (safely) convert the argument. My advice is: rework your function to accept non-mutable arguments if possible, if not - fix the type mismatch. @jsulm already pointed out that you shouldn't include headers you're not going to need, unless massive compilation times is something you're after.

                                    Kind regards.

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    0
                                    • RelaR Offline
                                      RelaR Offline
                                      Rela
                                      wrote on last edited by Rela
                                      #121

                                      Hi, the previous error was solved. I wanted to have a C++ code results in widget. For reminding in brief, I created a Qt widget project, and I added the C++ code including header and source files to the project. I copied the main.cpp C++ codes in the Qt main.cpp file.

                                      The error was ```

                                      void PS... (char *)': cannot convert argument 1 from 'const char [37]' to 'char *
                                      

                                      I changed the definition of the function:

                                      void PS... (char *_sth)
                                      

                                      to

                                      void PS... (const char *_sth) 
                                      

                                      and it doesn't give the error. But, now I want to have some values as result in Application Output , but instead of the non-zero values, they are all zero.
                                      The input data are read from a model.txt file. The path of the text file was also added in the "project" > run of "Desktop Qt 5.6 MSVC2015 64 bit" > "Arguments". Do you know if it is related to the reading input data or not?

                                      1 Reply Last reply
                                      0
                                      • jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #122

                                        How do you read model.txt file?
                                        Is this file found?
                                        Do you have any errors while reading it?
                                        Why don't just debug and see what happens?

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

                                        RelaR 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          How do you read model.txt file?
                                          Is this file found?
                                          Do you have any errors while reading it?
                                          Why don't just debug and see what happens?

                                          RelaR Offline
                                          RelaR Offline
                                          Rela
                                          wrote on last edited by
                                          #123

                                          @jsulm
                                          In the code, it read using ```

                                          bool ReadModelData(istream &in, functionname &_PS) ...
                                          ...
                                          ReadModelData (fin,s)
                                          ...
                                          

                                          The path of the text file was added in the "project" > run of "Desktop Qt 5.6 MSVC2015 64 bit" > "Arguments". File path
                                          When I debug it just show in the Application output:

                                          Debugging starts
                                          Debugging has finished
                                          

                                          and when run the code the results are cpu=0 z=0 ... while they should be some non-zero.

                                          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