我已經創建了下面的類爲QString:無法實現==操作符(),以自定義的類的對象比較
class videodup
{
public:
videodup(QString vid = "", int m_a = 0, int m_b = 0);
~videodup() {}
QString video;
bool operator==(const QString &str) const { return video == str; }
bool operator==(const videodup &dup) const {return video == dup.video;}
QList<matchPair> matches;
};
videodup::videodup(QString vid = "", int m_a = 0, int m_b = 0)
{
video = vid;
matches.append(matchPair(m_a, m_b));
}
我認爲這將允許我使用QList::contains()
一個QString
,但它給我一個錯誤:
/usr/local/Cellar/qt5/5.5.1_2/lib/QtCore.framework/Headers/qlist.h:981: error: invalid operands to binary expression ('videodup' and 'const videodup')
if (i->t() == t)
~~~~~~^~
/Users/phire/Documents/workspace/VideoTwin/matchpair.h:30: candidate function not viable: no known conversion from 'const videodup' to 'QString &' for 1st argument
bool operator==(QString &str) { return video == str; }
^
有問題的行是:
if (frame.videomatches.contains(vid))
ħ ERE是解釋上述
struct frm
{
QString file;
int position;
cv::Mat descriptors;
QList<videodup> videomatches;
};
QList<frm> frames;
void MainWindow::findDupes(frm &frame)
{
QString file = frame.file;
UMat mat = frame.descriptors.getUMat(cv::ACCESS_RW);
UMat indices;
UMat dists;
if (!mat.isContinuous() || mat.empty())
return;
QTime timestamp(0,0,0,0);
timestamp = timestamp.addMSecs(frame.position);
try
{
mat = mat.reshape(1,1);
index.knnSearch(mat,indices,dists,5);
}
catch (Exception e)
{
qWarning() << "index search failure" << e.err.c_str() << e.msg.c_str();
}
catch (exception& e)
{
qWarning() << "index search failure" << e.what();
}
// qDebug() << "indices cols" << indices.cols << "dists cols" << dists.cols;
db.transaction();
QSqlQuery matches(db);
QStringList tempmatches;
Mat indicesMat = indices.getMat(cv::ACCESS_READ);
Mat distsMat = dists.getMat(cv::ACCESS_READ);
for (int i = 0; i < indicesMat.cols; i++)
{
if (indicesMat.at<int>(0,i) == -1 || distsMat.at<int>(0,i) > 12800)
continue;
try
{
QTime matchtime(0,0,0,0);
int matchms = frames.at(indicesMat.at<int>(0,i)).position;
QString vid = frames.at(indicesMat.at<int>(0,i)).file;
matchtime = matchtime.addMSecs(matchms);
int temp = distsMat.at<int>(0,i);
tempmatches.append(QString::number(indicesMat.at<int>(0,i)));
if (frame.videomatches.contains(vid))
{
matchPair pair(frame.position, indicesMat.at<int>(0,i));
frame.videomatches[ frame.videomatches.indexOf(vid) ].matches.append(pair);
}
else
{
frame.videomatches.append(videodup(vid,frame.position, indicesMat.at<int>(0,i)));
}
// qDebug() << frame.file << "frame"<< timestamp.toString("hh:mm:ss") << "match at"<< vid << matchtime.toString("hh:mm:ss") << "distance" << temp;
}
catch (Exception e)
{
qWarning() << "failure in indices" << e.err.c_str() << e.msg.c_str() << e.func.c_str();
}
catch (exception &e)
{
qWarning() << "failure in indices" << e.what();
}
}
QString temp(tempmatches.join(","));
matches.prepare("UPDATE frames SET matches = :matches WHERE file = :file AND position = :position");
matches.bindValue(":matches",temp);
matches.bindValue(":file",frame.file);
matches.bindValue(":position",frame.position);
if (!matches.exec())
qWarning() << "couldn't add matches to frame in database";
db.commit();
}
我怎樣才能讓我的自定義類堪比QString
行代碼?
尼爾是相同的代碼,或自從它改變?你很遺憾地刪除了這個貼,我忘記了(首先它並不清楚)'frame','frame.videomatches'和'vid'是什麼。這很重要。 – iksemyonov
試圖只粘貼相關位http://pastebin.com/8WP8jT5P – Neal
好吧,我看到你的問題,想着如何規避它。使用STL很容易,Qt我需要考慮一下。 – iksemyonov