So in my code, I've used a method is_empty()in the boost library. I know is_empty has two definitions, one is in the filesystem library of boost:
std::filesystem:is_empty(const std::filesystem::path& p).
And another is in std::integral_constant:
inline constexpr bool is_empty_v = is_empty<T>::value;
I intended to use the one that is in the boost library, but how can I do it? I tried to not do using namespace std at the beginning. Instead, every time I need to cout, I'd use std::cout to avoid the possible ambiguous on my is_empty method. But I still got the same error if I do this. Is there any other way that can resolve this misconception? I attached part of my code snippet below. Thank you!
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "boost/filesystem.hpp"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
...
using namespace boost::filesystem;
using namespace cv;
... inside main() function:
...
directory_iterator itr(param.referFolder);
if( is_empty(itr->path()) )
{
std::cout << "ERROR: ReferenceFolder is empty. Please place reference images inside." << std::endl;
return 1;
}
... ...
for(directory_iterator itr(param.dataFolder); itr != end_itr; ++itr) // search every folder
{
if(is_directory(itr->status()) && !is_empty(itr->path())){
... ...
}
}
Related
my question is pretty simple but I can't seem to find it out.
I want to know what libary to include when using stoi. I was using atoi and it works fine with
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
but I get "stoi not declared" when I run with stoi. Thanks
You need to #include <string> and use a compiler that understands C++11. Minimal example:
#include <string>
#include <cassert>
int main()
{
std::string example = "1234";
int i = std::stoi(example);
assert(i == 1234);
return 0;
}
Compile, for example, with g++ -std=c++11.
I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}
I am trying to parse a json file within my program:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { get_current_dir_name() };
plan += "directory/file.json";
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
Error: Json File not found!
However when I manually write the entire path:
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/json.h>
#include <unistd.h>
#include <stdio.h>
int main(){
std::string plan { entire_file_path };
read_json(plan); // A function that reads a json file using jsoncpp
}
Output:
File found and read!
I thought maybe there is a spelling mistake but when I use std::cout on both of the paths, there is not a single difference. I'm not sure what is causing this issue.
Using std::filesystem built-in to C++17:
namespace fs = std::filesystem;
fs::path path = fs::current_path() / "directory" / "file.json";
read_json(path.string());
C++, Visual Studio 2019
I keep getting this error in release mode but not debug mode for lines that use the "filesystem" and "chrono" libraries. I'm thinking that the issue probably relates to how the header files are linked in release mode.
Below are the relevant snippets of my code (ellipses indicate omitted code). The red underline of the error is under "chrono" and "filesystem", respectively.
#include <iostream>
#include <vector>
#include <fstream>
#include <numeric>
#include <string>
#include <algorithm>
#include <ctime>
#include <iterator>
#include <filesystem>
#include <cmath>
using namespace std;
...
int main() {
auto startTime = chrono::steady_clock::now(); // ERROR HERE
...
for (auto& entry : filesystem::directory_iterator(AbsPath + PTS_ByIndustry_Path)) { // ERROR HERE
if (find(TS_ToIncludeNums.begin(), TS_ToIncludeNums.end(), i) != TS_ToIncludeNums.end()) {
string p = entry.path().u8string();
PTS_ToInclude.push_back(p);
}
i++;
}
...
}
Fixed already! In project properties, I was changing the language standard to C++17 for Debug Mode but not Release Mode.
Screenshot:
.
It seems you didn't import the chrono header.
#include <chrono>
If I run the code the error occurs: Segmentation fault (core dumped)
The problem does not exist if I decrease the dimensions of MatSamplesDimM in the header-file (e.g. from 300000 to 30000) or if I just declare Matrix1 and not Matrix2 in the main-file. The problem is independent of environment in which I run the code (eclipse or from terminal).
Do you have any ideas? Thank you very much for your help!
StrangeBehavior.cpp is my main-File:
// System includes
#include <iostream>
#include <fstream> //fstream: Stream class to both read and write from/to files; for output
// Boost includes
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/math/distributions/normal.hpp> //to create normal distribution
#include <vector> // stl vector header
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp> //accumulator for mean, variance...
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/bind/bind.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/matrix_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/vector_expression.hpp> // for using inner_prod
#include <boost/numeric/ublas/matrix_expression.hpp> // for using inner_prod, element_div
#include <boost/math/special_functions/gamma.hpp> // for Gamma function
#include "ParameterDeclaration.hpp"
using namespace std; // saves us typing std:: before vector
int main() {
std::cout << "Starting " << std::endl;
MatSamplesDimM Matrix1;
MatSamplesDimM Matrix2;
std::cout << "Finishing" << std::endl;
return 0;
}
and ParameterDeclaration.hpp my header-File:
#ifndef parameter_
#define parameter_
#include <iostream>
namespace ublas = boost::numeric::ublas;
typedef ublas::bounded_matrix<double,300000,2> MatSamplesDimM;
#endif
Try using the heap instead of the stack. This means allocate the matrices using new.