0
我試圖爲我的容器實現operator []。但我對C++真的很陌生,看起來我的實現中有一個錯誤。」錯誤:預期在'float'之前的非限定id'「對於運算符[] overload
我宣佈他們是這樣的:
float& operator[](const int &idx);
const float& operator[](const int &idx) const;
這應該是罰款,這是從教程幾乎複製/粘貼。現在,Quaternion.cpp看起來是這樣的:
float& Quaternion::operator[](const int &idx)
{
if(idx == 0)
{
return x;
}
if(idx == 1)
{
return y;
}
if(idx == 2)
{
return z;
}
if(idx == 3)
{
return w;
}
std::cerr << "Your Quaternion is only accessible at positions {0, 1, 2, 3}!"
<< std::endl;
return x;
}
const float& Quaternion::operator[](const int &idx)
{
if(idx == 0)
{
return const x;
}
if(idx == 1)
{
return const y;
}
if(idx == 2)
{
return const z;
}
if(idx == 3)
{
return const w;
}
std::cerr << "Your Quaternion is only accessible at positions {0, 1, 2, 3}!"
<< std::endl;
return x;
}
我得到錯誤的簽名 「常量浮動&四元數::運算符[](const int的& IDX)」。
之前發生的另一件事是,如果邊界超出限制,我無法返回0。也許我會,一旦這個問題得到解決,但它給了我一個錯誤信息之前。我只是返回了x,這讓我非常不高興。
你的簽名不匹配。 – chris
從教程複製/粘貼就像在街上吃口香糖一樣。不要這樣做。 –