假設我有一個ComplexNumber看起來像這樣的模板類:
template<typename T>
class ComplexNumber {
public:
ComplexNumber() : A(), B() {}
ComplexNumber(const T& r, const T& i) : A(r), B(i) {}
~ComplexNumber(){}
void setA(T A1);
void SetB(T B1);
T getA() const {return A;};
T getB()const {return B;};
ComplexNumber<T> operator (const ComplexNumber<T> &C){
return ComplexNumber<T>( A C.getA(), B C.getB());
}
ComplexNumber<T> operator -(const ComplexNumber<T> &C) {
return ComplexNumber<T>(A - C.getA(), B - C.getB());
};
friend ostream & operator << (ostream &out, const ComplexNumber<T> &c)
{
out << c.A;
out << " i" << c.B << endl;
return out;
}
private:
T A;
T B;
};
和 amain()構造這些復數并將它們存盤到 a 中std::vector:
ComplexNumber<int> complex1(10, 3);
ComplexNumber<int> complex2(2, 56);
ComplexNumber<int> complex3(3, 55);
vector<ComplexNumber<int>> testVector;
testVector.push_back(complex1);
testVector.push_back(complex2);
testVector.push_back(complex3);
如果我想testVector從最高到最低排序,比較實部,然后在實部相等的情況下比較虛部,我該怎么做?
我無法使用標準std::sort()功能。我想使用方法或仿函式來做到這一點。
編輯:嘗試將其添加到函式中:
//This method is outside the scope of the ComplexNumber class
auto compare_by_magnitude = [](const auto& a, const auto& b) {
return a.A*a.A a.B*a.B < b.A*b.A b.B*b.B;
};
void sortComplex(vector<ComplexNumber<int>> &c){
std::sort(c.begin(),c.end(),compare_by_magnitude);
}
我的錯誤資訊:
FAILED: complexNumber.exe
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.3\bin\mingw\bin\G__~1.EXE -g CMakeFiles/complexNumber.dir/main.cpp.obj CMakeFiles/complexNumber.dir/ComplexNumber.cpp.obj -o complexNumber.exe -Wl,--out-implib,libcomplexNumber.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2021.3.3\bin\mingw\bin/ld.exe: CMakeFiles/complexNumber.dir/ComplexNumber.cpp.obj: in function `ComplexNumber<int>::~ComplexNumber()':
C:/PROGRA~1/JETBRA~1/CLION2~1.3/bin/mingw/lib/gcc/x86_64-w64-mingw32/11.2.0/include/c /bits/stl_heap.h:223: multiple definition of `sortComplex(std::vector<ComplexNumber<int>, std::allocator<ComplexNumber<int> > >&)'; CMakeFiles/complexNumber.dir/main.cpp.obj:C:/Users/elira/Desktop/complexNumber/complexNumber/ComplexNumber.h:51: first defined here
C:\Program Files\JetBrains\CLion 2021.3.3\bin\mingw\bin/ld.exe: CMakeFiles/complexNumber.dir/ComplexNumber.cpp.obj:C:/Users/elira/Desktop/complexNumber/complexNumber/ComplexNumber.h:48: multiple definition of `compare_by_magnitude'; CMakeFiles/complexNumber.dir/main.cpp.obj:C:/Users/elira/Desktop/complexNumber/complexNumber/ComplexNumber.h:48: first defined here
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
uj5u.com熱心網友回復:
為了使用std::sort(),您的班級至少需要operator<定義:
bool operator< (const ComplexNumber<T>& other) const {
return A < other.A || (A == other.A && B < other.B);
}
定義 也是一種很好的做法operator==,但std::sort()可以operator<單獨使用。
當我在您的課程中添加上述內容時,這main()是按實數和虛數按升序排列的:
int main() {
vector<ComplexNumber<float>> vc = { {4, 5}, {1, 3}, {4, 2} };
copy(vc.begin(), vc.end(), ostream_iterator<ComplexNumber<float>>(cout, " "));
cout << endl;
sort(vc.begin(), vc.end());
copy(vc.begin(), vc.end(), ostream_iterator<ComplexNumber<float>>(cout, " "));
cout << endl;
}
但是,您提到要按降序排序。operator<正如您可以想象的那樣,在類中反向定義是不好的做法,因此您可以傳遞一個比較函式來std::sort()反轉比較的含義:
sort(vc.begin(), vc.end(),
[](const ComplexNumber<float> &f1, const ComplexNumber<float> &f2)
{ return ! (f1 < f2); }
);
除了這些編譯和語意問題之外,當我查看您發布的錯誤時,我看到了一個鏈接錯誤,并且您的類似乎被定義了多次。這表明定義您的類的包含檔案缺少包含保護。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/473680.html
上一篇:僅在按下按鈕時使用克隆助手
