I think that the default overload of == for valarray is not very convenient. By default x==y (for two valarrays x and y) returns a valarray<bool>, with true on the ith entry if x[i]==y[i]. Rather, I need a single bool, which tells me if both valarray<double> contain the same elements or not. I know I can do this with a cycle, but having to do the cycle every time is not convenient. What's the best workaround here? Is there a way for me to define my own overload of == (and also !=, <, and so on)?
net
lundi 29 juin 2015
C++ fstream is writing hex instead of string?
I'm trying to download a file using URLDownloadToFile() which is working so far, however I'm having trouble with the callback function and writing the callback output wszStatusText to a file.
Here is the function that's giving the problem:
HRESULT DownloadStatus::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR wszStatusText)
{
fstream myfile;
myfile.open("file.txt", ios::app);
// this prints hex e.g. StatusText: 00435F78
myfile << " StatusText: " << wszStatusText;
myfile.close();
// this prints the string properly e.g. text/plain
MessageBox(NULL, wszStatusText, L"test", MB_OK);
return S_OK;
}
The thing is that MessageBox() is showing the data properly...
How to declare unique_ptr of vector?
I am trying to declare a global vector of MyClass using unique_ptr. My compiler is 4.8.4.
glo.h
extern std::unique_ptr<std::vector<MyClass>> gl_vec;
glo.cpp
std::unique_ptr<std::vector<MyClass>> gl_vec;
And in the file where I initialize and use it for the first time in a different *.cpp file:
#include "glo.h"
// within function
{
gl_vec = std::unique_ptr<std::vector<MyClass>> ();
cout << "gl_vec size = " << (*gl_vec).size() << endl; // crashes here
}
Things keep crashing when I use the pointer. Anyone see what I'm doing wrong?
Qt QDir::current()
I had some code like this:
void MainWindow::saveData()
{
QDir oldDir=QDir::current();//this should return the main executable directory.Since there is no other place in my hole code where i temper with QDir.
QDir sess("Sessions");
if(!oldDir.exists("Sessions"))//if "Sessions" Dir doesn't exist
oldDir.mkdir("Sessions");//create it.
QDir::setCurrent(sess.absolutePath());
//some virtual code inside current Dir, which i didn't implement yet.
QDir::setCurrent(oldDir.absolutePath());//restore old dir
}
When i run my app firstly the code works perfectly.but in the second run, the first call to "QDir::current();" returns the "Sessions" Dir and not the main executable Dir as it should be restored in the first run.actually i did manage to overcome this by adding one line at the biginning of the code, the following :
QDir::setCurrent(QCoreApplication::applicationDirPath());
Still i want to know why the first code didn't work.already checked for the documentation of the functions and found nothing.
gmtime_r((time_t*)&title->start_time, &start_time);
I'm trying to compile on Microsoft visual studio 2013 on C++ a program written for linux ( is a mix of C and C++ (#include .h) and I'm going to convert all in C++ to not be more confused !)
the statement:
gmtime_r((time_t*)&title->start_time, &start_time);
return errors: Error 11 error C3861: 'gmtime_r': identifier not found IntelliSense: identifier "gmtime_r" is undefined
please help
Passing a reference-to-function as a universal reference
I'm struggling to understand what exactly happens when passing a reference-to-function to a function as a universal reference (what type is being deduced). Let's suppose we have a function foo that takes a param as a universal reference:
template<typename T>
void foo(T&& param)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
And then let's do the following:
void(&f)(int) = someFunction;
foo(f);
The result will be:
void foo(T&&) [with T = void (&)int]
This is perfectly understandable: we are passing lvalue to our function foo, so the deduced type is void(&)int, and the type of the param will be "void(&& &)int" which under reference collapsing rules becomes void(&)int. Param will be just an lvalue reference to a function.
But when I do the following:
void(&f)(int) = someFunction;
foo(std::move(f));
foo will print:
void foo(T&&) [with T = void (&)int]
which is exactly the same as before! What is happening here? Why the result is the same as when passing lvalue? I would expect that since we are passing rvalue to foo, the deduced type should be T = void(int), and param should become void(&&)int. This always happen with all other "normal" types (like classes, primitive types, etc.) Why is it different when dealing with function references?
Refresh a Combobox in C++?
I have a function that detects camera ports in 3D slicer, however it seems to only run once. When I unplug/plug in a camera, the number of ports should update in a combobox (designed in Qt), but nothing changes.
The function I'm using detects when the camera port is clicked:
void qSlicerTrackingModuleWidget::onCameraPortClicked(){
Q_D(qSlicerTrackingModuleWidget);
// Clear current entries
d->CameraPortComboBox->clear();
int n = 0;
// Loop over camera ports until last one is found. Add all available ports to combo box and exit.
while(1){
cv::VideoCapture cap = cv::VideoCapture(n);
if(!cap.isOpened()){
return;
}
QString portNum = QString::fromStdString(std::to_string(n++));
d->CameraPortComboBox->addItem(portNum);
qSlicerCoreApplication::processEvents();
}
}
The setup function runs last and assigns the GUI to the actual function.
connect( d->CameraPortComboBox, SIGNAL(clicked()), this, SLOT(onCameraPortClicked()));
I need it to refresh and try to detect the cameras every time the combobox is clicked on, but because of the interface setup I am not sure if it is possible. I don't think constantly refreshing the program is a good option, so I'm out of ideas. Is there any way to do this?