This is not a Most Vexing Parse problem, because doing the following will also result in linker errors although there's no ambiguity:
QImage baseline("C:\img1.png");
QImage test_image("C:\img1.png");
My application gets 2 command line arguments which are paths to 2 different images.
I would like to convert them to QImage after so I can compare them using the ==
operator Qt provides.
I have tried the following ways to go about it but I get linker issues every time:
Attempt 1:
QImage baseline(argv[1]);
QImage test_image(argv[2]);
Attempt 2 (convert to Qstring first):
QString base_path = QString::fromStdString(argv[1]);
QString test_path = QString::fromStdString(argv[2]);
QDir baseline(base_path);
QDir test_image(test_path);
I don't think this is related to not having the correct library included because if I simply do the following, the program compiles and links fine:
Qimage test_image();
Already looked at the thread below, unfortunately it wasn't very helpful in my case: Constructing QImage from unsigned char* data
Any thoughts on what could be causing the linker errors? I doubt they'd be very helpful but I'll leave the linker error messages here:
Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) public: __thiscall QString::~QString(void)" (__imp_??1QString@@QAE@XZ) referenced in function _main image_compare_executable c:\Users\gela8178\documents\visual studio 2015\Projects\image_compare_executable\image_compare_executable\image_compare_executable.obj
1 Error LNK2019 unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromStdString(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_?fromStdString@QString@@SA?AV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main image_compare_executable c:\Users\gela8178\documents\visual studio 2015\Projects\image_compare_executable\image_compare_executable\image_compare_executable.obj
1 Error LNK1120 2 unresolved externals image_compare_executable c:\users\gela8178\documents\visual studio 2015\Projects\image_compare_executable\Debug\image_compare_executable.exe 1
Including the relevant snippet of my code causing the linker issues:
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <QtGui/QtGui>
using namespace cv;
int main(int argc, char* argv[])
{
// Check the number of parameters
if (argc < 3) {
std::cerr << "Error: " << argv[0] << " : " << "Missing arguments" << std::endl;
return 1;
}
std::cout << "Baseline: " << argv[1] << "\n" << "Test Image: " << argv[2] << std::endl;
QImage baseline(QString(argv[1]));
QImage test_image(QString(argv[2]));
if (baseline == test_image)
{
std::cout << "Algorithm: Mem Compare" << "\n" << "Result: = Pass" << std::endl;
}
else
{
std::cout << "Algorithm: Mem Compare" << "\n" << "Result: = Fail" << std::endl;
}
std::cout << "--------------------------------------------------------" << std::endl;
// ***************************************************************************** //
//Run MSSIM and output tuple
Mat i1 = imread(argv[1]);
Mat i2 = imread(argv[2]);
Scalar min_ssim = getMSSIM(i1, i2);
double R = min_ssim[0];
double G = min_ssim[1];
double B = min_ssim[2];
std::cout << "Algorithm: MSSIM" << "\n" << "Result: " << "(" << R << ", " << G << ", " << B << ")" << std::endl;
std::cout << "--------------------------------------------------------" << std::endl;
return 0;
}
SOLUTION:
Looking at the QImage constructors here the only instance that takes a QString takes it as a const reference
:
QImage(const QString &fileName, const char *format = Q_NULLPTR)
Doing the following
QImage baseline(QString(argv[1]));
was throwing linker errors because the QString was being passed by r reference, being destroyed by the time QImage was trying to access it. So the possible fixes would be to either do the QString assignment separately and then pass it to QImage so just do:
QImage baseline(argv[1]);
QImage test_image(argv[2]);