How to run a C++ code in Qt?



  • Thanks it worked today, the run button was active and gave "Hello world" in widget.
    Just I am doing the same with another longer C++ code. I copied all .h and .cpp files to the created Qt project folder and added using
    "Add existing file" to Qt .h and .cpp files. But instead of main.cpp in Qt, I copied the .cpp code in my PS C++ project. When I run it it gives more than 10 errors like

    
    "\\home.org.a\ab\data\Documents\Qt projects\PS7\main.cpp:1060: error: C2664: 'void PSError(char *)': cannot convert argument 1 from 'const char [37]' to 'char *'' in main.cpp file.
    
    

    I changed the the #include <tchar.h> to #include <char.h> in "stdafx.h" file, but the error decreased to two including ```

    "\\home.org.a\ab\data\Documents\Qt projects\PS7\stdafx.h:11: error: C1083: Cannot open include file: 'char.h': No such file or directory"
    
    
    PS. May I ask why I cannot see my previous posts in this page? I need sometimes to look at them, how can I do that?

  • Lifetime Qt Champion

    Just to be clear, are you trying to build MSVC generate code with a MinGW build of Qt ?
    If so why not just import the code you wrote in your VS project in your Qt project and dump the VS specific files that you anyway will not use in your Qt project ?

    If you have the infinite scroll activated, just scroll up otherwise you have a at the bottom a page selector.



  • @SGaist
    I think I wrote by mistake. I am trying to run my C++ project using Qt Widget project.
    I have these files VS I copied them in my Qt project folder, and added stdafx.cpp, stdafx.h, targetver.h and O_PS .H to the Qt. Instead of adding "O_PS .cpp" I copied its content to the "main.cpp" in Qt. For "po .cpp" also I just added
    #include "stdafx.h" to the "mainwindow.cpp". Now I have these files in Qt: Qt

    The errors were explained in the previous post.


  • Qt Champions 2018

    See here: http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio
    stdafx.h seems to be generated by VS, so you should not use it with MinGW



  • @jsulm
    If I understood your post, I dont run the Qt with MinGW, here I run it using Desktop MCVS 2015 compiler, after adding the path of the input data file to the Run of the MCVS 2015 in the project.



  • Could you please tell your ideas about my previous question posted 6 days ago?


  • Qt Champions 2018

    Please take a look at error messages you get:

    "\\home.org.a\ab\data\Documents\Qt projects\PS7\main.cpp:1060: error: C2664: 'void PSError(char *)': cannot convert argument 1 from 'const char [37]' to 'char *'' in main.cpp file.
    

    Now check the code lines mentioned in these error messages. Error message you posted tells you that you are passing a const char array to a function that takes a non-const char*. This is not allowed.



  • But previously it did not give that error, when I did the same with different versions of Qt5.5 and MCVS 2013. The number of errors

     cannot convert argument 1 from 'const char [37]' to 'char *''
    

    is about 12-13.
    However, when I change the

    #include <tchar.h>
    

    to

    #inclu```de <char.h>
    

    in "stdafx.h" file, the error decreased to two including

    \\home.org.a\ab\data\Documents\Qt projects\PS7\stdafx.h:11: error: C1083: Cannot open include file: 'char.h': No such file or directory
    

    errors.



  • Could you please reply me?



  • @Rela

    I don't remember that there is any char.h file.



  • @the_
    Thanks, could you please read my last 6 previous posts?



  • I did the same process to read the C++ project in Qt Creator, but this time the error is ```

    :-1: error: dependent '..\C++' does not exist.
    

    These are my C++ project files:
    O_PS.H
    stdafx.h
    targetver.h
    O_PS.CPP
    ps.cpp
    stdafx.cpp

    and I built a mainwindow Qt creator project (PS8) and copied all the files in the Qt folder except "ps.cpp" and "O_PS.CPP". The copied ones were added to the Qt project using "Add Existing Files". There are some differences including:
    1- The "ps.cpp" contains only ```

    #include "stdafx.h"
    

    which was added to "mainwindow.cpp".
    2- "O_PS.CPP" content was copied in the "main.cpp" of the Qt project. The Qt PS8 project now includes these files.


  • Lifetime Qt Champion

    I'm going to repeat myself but: why not just start from a clean Qt project, copy the code you need to run over in that project and go on ?

    You have some core logic in that visual studio project but rather than just take the current working code and move it over to your Qt project with maybe some adaptations, you are trying to grab every bits of that project and mix it with your Qt project when there's no real need.



  • @SGaist
    I almost did the same, except I added the files "stdafx.h", "targetver.h", "stdafx.cpp", and "O_PS .H", which I think are needed. Because in this C++ code, only the "O_PSA .H" and "O_PS .CPP" include code.
    The content of "stdafx.h" is #include```

     "targetver.h"
    #include <stdio.h>
    #include <tchar.h>
    

    The content of "targetver.h" is ```

    #include <SDKDDKVer.h>
    

    The content of "stdafx.cpp" is ```

    #include "stdafx.h"
    

    And the content of "po .cpp" is ```

    #include "stdafx.h"
    

    But, if you mean I just copy the "O_PSA .H" and "O_PS .CPP" codes in "mainwindow.h" and "main.cpp", (here is the Qt project it gives many errors, and I think because all cpp and header files are necessary.


  • Lifetime Qt Champion

    You have to fix the includes in your O_PS* files so add the missing #include <stdio.h>



  • @SGaist
    Do I add #include <stdio.h> to "mainwindow.h" ? I added to all files one by one , but the same errors happen.
    Do I need to add #include "stdafx.h" or something else also?


  • Lifetime Qt Champion

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



  • @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


  • Qt Champions 2017

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



  • @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.


  • Qt Champions 2017

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



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

    #include <stdio.h>
    

    to the "mainwindow.h"?


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



  • @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.
    

  • Qt Champions 2017

    @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


  • Lifetime Qt Champion

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



  • @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


  • Qt Champions 2018

    What is char.h?



  • @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[])
    {
    

  • Qt Champions 2018

    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);
    

  • Qt Champions 2017

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



  • 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?


  • Qt Champions 2018

    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?



  • @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.


Log in to reply