ifstream no matching constructor for initialization - c++

I'm using QT Creator and MinGW (both latest versions) and am having trouble getting the ifstream to use the path argument constructor added in c++17.
Compiling the below code will fail with:
no matching constructor for initialization of 'std::ifstream'
I've got CONFIG += c++17 in my QT .pro file and LIBS += -lstdc++fs
MCV
https://gcc.godbolt.org/z/Lb3MNT
#include <experimental/filesystem>
#include <fstream>
int main() {
const std::experimental::filesystem::path my_path = "C:/";
std::ifstream input_file_stream(my_path);
}

# user1406186, I replicated your same error, and was able to compile it applying the following changes to the .pro file and had to specify the QMAKE I needed to use:
TEMPLATE = app
CONFIG += console c++11
QMAKE_CXXFLAGS += -std=gnu++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
HEADERS +=
LIBS += -lstdc++fs
It also compiled with the following C++14/C++11 standards:
TEMPLATE = app
CONFIG += console c++14
QMAKE_CXXFLAGS += -std=gnu++14
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
HEADERS +=
LIBS += -lstdc++fs

#include<string>
#include<experimental\filesystem>
using namespace std;
using namespace experimental::filesystem;
int main()
{
path soure{current_path()};
soure /="test.txt";
ifstream input{u8path(soure).u8string()};
if(!input)
{
cout<<"source file not exist"<<endl;
}
path dest{current_path()};
dest /="test-copy.txt";
ofstream output{u8path(dest).u8string()};
string line;
while(!getline(input,line).eof())
{
output<<line<<endl;
}
input.close();
output.close();
return 0;
}

Related

Qt + Ogre3D. I cannot build a very simple example

I use:
Qt 5.15.2 MinGW 32-bit Dynamic for Debug build
Ogre 13.1.1 MinGW Static build
There is a class called ApplicationContextQt that can help to use Qt with Ogre3D but I have errors:
This example is very simple:
main.cpp
#include "Ogre.h"
#include "OgreApplicationContextQt.h"
#include <QGuiApplication>
class MyTestApp : public OgreBites::ApplicationContextQt, public OgreBites::InputListener
{
public:
MyTestApp();
void setup();
bool keyPressed(const OgreBites::KeyboardEvent& evt);
};
MyTestApp::MyTestApp() : OgreBites::ApplicationContextQt("OgreTutorialApp")
{
}
bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)
{
if (evt.keysym.sym == OgreBites::SDLK_ESCAPE)
{
getRoot()->queueEndRendering();
}
return true;
}
void MyTestApp::setup(void)
{
// do not forget to call the base first
OgreBites::ApplicationContextQt::setup();
}
int main(int argc, char *argv[])
{
QGuiApplication qapp(argc, argv);
MyTestApp app;
app.initApp();
app.getRoot()->startRendering();
app.closeApp();
return qapp.exec();
}
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE"
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\Bites"
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\RTShaderSystem"
LIBS += -L"E:\ProgramFiles\ogre-13.1.1\lib"
LIBS += -L"E:\ProgramFiles\ogre-13.1.1\lib\OGRE"
LIBS += -lOgreBitesQtStatic
LIBS += -lOgreBitesStatic -lRenderSystem_GL3PlusStatic -lOgreGLSupportStatic -lOgreMeshLodGeneratorStatic -lOgreOverlayStatic -lOgrePagingStatic -lOgrePropertyStatic -lOgreRTShaderSystemStatic -lOgreVolumeStatic -lPlugin_BSPSceneManagerStatic -lPlugin_DotSceneStatic -lPlugin_OctreeSceneManagerStatic -lPlugin_OctreeZoneStatic -lPlugin_ParticleFXStatic -lPlugin_PCZSceneManagerStatic -lRenderSystem_GLES2Static -lRenderSystem_GLStatic -lOgreMainStatic -lOgreTerrainStatic -lCodec_STBIStatic -lzlibstatic
LIBS += -lSDL2main -lSDL2.dll -lfreetype -lpugixml
LIBS += -lopengl32 -lgdi32
You can try this one ?
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE"
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\Bites"
INCLUDEPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\RTShaderSystem"
DEPENDPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE"
DEPENDPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\Bites"
DEPENDPATH += "E:\ProgramFiles\ogre-13.1.1\include\OGRE\RTShaderSystem"
LIBS += -L"E:/ProgramFiles/ogre-13.1.1/lib/OgreBitesQtStatic" -lOgreBitesQtStatic
LIBS += -L"E:/ProgramFiles/ogre-13.1.1/lib/OGRE" -lOgreBitesStatic
// OR ...
LIBS += "E:/ProgramFiles/ogre-13.1.1/lib/OgreBitesQtStatic/OgreBitesQtStatic.a"
LIBS += "E:/ProgramFiles/ogre-13.1.1/lib/OGRE/OgreBitesStatic.a"
// It just depends on the actual names of your libraries
// Same for the others unless their file location is decleared under system path.
// If so, you can add that libraries like how you did with opengl
And it is nice to add PRE_TARGETDEPS macros too if you make changes on those libraries so often:
PRE_TARGETDEPS += "E:/ProgramFiles/ogre-13.1.1/lib/OgreBitesQtStatic.a"
You can replace "/"s with "" too.
And the problem with your pro file was: You added library paths as libraries. And then tried to add that libraries from system environment.

OpenCV and Qt 5.3 weird message

I'm trying to run (in Qt, C++) the following code that uses OpenCV:
.pro file:
QT += core
QT -= gui
TARGET = testOpenCV2
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv\release\bin\libopencv_core300.dll
LIBS += C:\opencv\release\bin\libopencv_highgui300.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs300.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc300.dll
main.cpp file:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread("pic.jpg", CV_LOAD_IMAGE_COLOR);
cv::namedWindow("My Image", WINDOW_AUTOSIZE);
cv::imshow("My Image", image);
cv::waitKey(0);
return 0;
}
But I'm getting the following message:
exited with code -1073741515
And sometimes I get the following message:
Cannot obtain a handle to the inferior: The parameter is incorrect.
and nothing is happening.
Can anyone help me please?
I'm new in Qt and I don't know what's going on.
You are not using Qt at all (except you try link QtCore). For running a QtApplication you would at least need to call QCoreApplication.
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Some code here
// QMainWindow w;
// w.show();
return a.exec();
}
Creating a non Qt application you would change your pro-file to (omit the QT at all):
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv\release\bin\libopencv_core300.dll
LIBS += C:\opencv\release\bin\libopencv_highgui300.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs300.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc300.dll
This way you also won't need ''QCoreApplication''

libjpegTurbo: libjpeg-62 File not recognized

I tried to use libjpeg-turbo with qt.
I downloaded libjpeg-turbo and installed. I wanted to use it within a project but I got the following fault:
C:\libjpeg-turbo-gcc64\bin\libjpeg-62.dll:-1: Error: file not
recognized: File format not recognized
As soon as I removed the libjpeg-62.dll I received the following fault:
C:\test\main.cpp:8: Error: undefined reference to
`tjInitCompress'
Why is the libjpeg-62 not recognizing the file format?
Thanks for help,
Willy
PS. Here is the code:
test.pro
QT += core
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\libjpeg-turbo-gcc64\include
LIBS += -LC:\libjpeg-turbo-gcc64\bin -llibjpeg-62
main.cpp
#include <QCoreApplication>
#include <turbojpeg.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
tjhandle _jpegCompressor = tjInitCompress();
return a.exec();
}
Ok now it works,
it was the wrong version of libjpeg-turbo. Now I use the libjpeg-turbo-gcc and not the libjpeg-turbo-gcc64. Also i change the Libs-Path to
LIBS += "C://libjpeg-turbo-gcc64//bin//libjpeg-62.dll"
MfG Willy

Qt Creator on Mac and boost libraries

I am running QtCreator on Mac... I want to start working on boost libraries ... So, I installed boost libraries using
brew install boost
After that I created a small boost hallo world program and made the changes in .pro file as follows
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
unix:INCLUDEPATH += "/usr/local/Cellar/boost/1.55.0_1/include/"
unix:LIBPATH += "-L/usr/local/Cellar/boost/1.55.0_1/lib/"
SOURCES += main.cpp
LIBS += \
-lboost_date_time \
-lboost_filesystem \
-lboost_program_options \
-lboost_regex \
-lboost_signals \
-lboost_system
I am still unable to build... What could be the reason? Please suggest me what could be the possible mistake...
The errors are
library not found for -lboost_data_time
linker command failed with exit code 1 (use -v to see invocation)
This is taking a bit from Uflex's answer, as he missed something.
So keep the same code:
//make sure that there is a boost folder in your boost include directory
#include <boost/chrono.hpp>
#include <cmath>
int main()
{
auto start = boost::chrono::system_clock::now();
for ( long i = 0; i < 10000000; ++i )
std::sqrt( 123.456L ); // burn some time
auto sec = boost::chrono::system_clock::now() - start;
std::cout << "took " << sec.count() << " seconds" << std::endl;
return 0;
}
But lets change his .pro a bit:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
macx {
QMAKE_CXXFLAGS += -std=c++11
_BOOST_PATH = /usr/local/Cellar/boost/1.55.0_1
INCLUDEPATH += "$${_BOOST_PATH}/include/"
LIBS += -L$${_BOOST_PATH}/lib
## Use only one of these:
LIBS += -lboost_chrono-mt -lboost_system # using dynamic lib (not sure if you need that "-mt" at the end or not)
#LIBS += $${_BOOST_PATH}/lib/libboost_chrono-mt.a # using static lib
}
The only thing I have added to this was the boost system( -lboost_system )
That should solve the issue with his original version causing the undefined symbols, and allow you to add your other libraries.
Such as -lboost_date_time, which for me worked perfectly with the brew install.
Granted, my path is actually: /usr/local/Cellar/boost/1.55.0_2
Boost libraries are modularized, you just need to link against the libraries that you are using. Some libraries are header only, so you don't need to do anything, having boost reachable in your path is enough.
You can try to compile this:
//make sure that there is a boost folder in your boost include directory
#include <boost/chrono.hpp>
#include <cmath>
int main()
{
auto start = boost::chrono::system_clock::now();
for ( long i = 0; i < 10000000; ++i )
std::sqrt( 123.456L ); // burn some time
auto sec = boost::chrono::system_clock::now() - start;
std::cout << "took " << sec.count() << " seconds" << std::endl;
return 0;
}
And in the .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
macx {
QMAKE_CXXFLAGS += -std=c++11
_BOOST_PATH = /usr/local/Cellar/boost/1.55.0_1
INCLUDEPATH += "$${_BOOST_PATH}/include/"
LIBS += -L$${_BOOST_PATH}/lib
## Use only one of these:
LIBS += -lboost_chrono-mt # using dynamic lib (not sure if you need that "-mt" at the end or not)
#LIBS += $${_BOOST_PATH}/lib/libboost_chrono-mt.a # using static lib
}

How to use boost regex with qt creator and msvc

I had VS 2010 installed already installed in my system.
So when i downloaded QT (I have to use QT as thats what the project req was in) ,i used this link and installed it.
It was able to auto detect the visual C++ compilers and was working fine.
Now I downloaded boost library from boost.org and installed using the following commands from visual studio command prompt:-
> bootstrap.bat msvc
>
> c:\boost_1_54_0>b2 install --prefix=c:/boostinst toolset=msvc-10.0
> variant=debug ,release link=static threading=multi
after that i opened qt creator and added the following code cpp file
#include <boost/regex.hpp>
#include
#include
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
and added the library using ADD Library and the following .pro file was generated.
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += C:\boostinst\include\boost-1_54 #if i remove this line, then also the same error
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../boostinst/lib/ -llibboost_regex-vc100-mt-1_54
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../boostinst/lib/ -llibboost_regex-vc100-mt-1_54d
else:unix: LIBS += -L$$PWD/../../../boostinst/lib/ -llibboost_regex-vc100-mt-1_54
INCLUDEPATH += $$PWD/../../../boostinst/include
DEPENDPATH += $$PWD/../../../boostinst/include
When i try to build , it throws the following error
C:\Users\xxx\newcp\main.cpp:24: error: C1083: Cannot open include file: 'boost/regex.hpp': No such file or directory
Am i Missing something or doing something wrong? Please anyone respond as soon as possible.
SOLVED: Use the following commands for Building boost_154_00 in 32-bit OS Win7 and msvc-10.0
> cd C:\boost_1_54_0\tools\build\v2\engine
> build.bat msvc
>
> cd boost_1_54_0
>
> set PATH=%PATH%;C:\boost_1_54_0\tools\build\v2\engine\bin.ntx86
>
> bjam toolset=msvc-10.0
Then in QT create a new project and paste in main.cpp
#include <QCoreApplication>
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
return a.exec();
}
in .pro add
INCLUDEPATH+=C:\boost_1_54_0
LIBS+=-LC:\boost_1_54_0\stage\lib\
Follow directions here
and then add arguments in qt project->run->arguments