2
我有一個模板類實現了紅黑樹,其行爲就像(僞)地圖。僞映射,因爲它不會存儲鍵,只有值,並且鍵中嵌入了值。這將要求運營商==和運營商<重載測試存儲的值對任意鍵和再次彼此(使用嵌入式密鑰)。例如。少於模板參數的比較
struct Val
{
//some actual data
std::string key;
bool operator==(const Val &val) { return this->key == val.key; }
bool opeartor==(const std::string &str) { return this->key == str; }
bool opeartor<(const Val &val) { return this->key < val.key; }
bool opeartor<(const std::string &str) { return this->key < str; }
};
這種「價值」會進入這個模板類:
template<typename T>
class Map
{
struct Node
{
//...
T data;
};
public:
//..
template<typename K> T value(const K &key) const
{
Node *it;
//...
if(it->data == key)
//...
int dir = (it->data < key);
//...
}
};
但是同時行if(it->data == key)
檢查了罰款(我不使用類還),第二個int dir = (it->data < key);
不錯誤「模板參數列表中的解析錯誤」。奇怪的是,如果我改變比較<=
它編譯好。然而,在這一點上我已經知道這是不是等於(第一檢查,以便<
會做。
如何解決呢?它爲什麼抱怨一個運營商,而不是其他人?
沒有語法錯誤,它編譯罰款給我。 –
可能是https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10200。你有一個叫做'data'的模板嗎? –
@ T.C。是的,哇!這是超級怪異的。改變了名稱,它的作品。您能否請將此重新發布爲答案?謝謝,這節省了我很多徒勞的嘗試來解決它。沒辦法,我可以自己解決這個問題。編譯器中的錯誤,嗯。 – Resurrection