运算符 [] 重载的"error: expected unqualified-id before 'float' "
"error: expected unqualified-id before 'float' " for operator[] overload
>我试图为我的一个容器实现运算符[]。但是我对c ++真的很陌生,似乎我的实现中有一个错误。
我是这样宣布的:
float& operator[](const int &idx);
const float& operator[](const int &idx) const;
这应该没问题,它几乎是从教程中复制/粘贴的。现在,四元数.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 float&Quaternion::operator[](const int &idx)"的错误。
之前发生的另一件事是,如果超出边界,我无法返回 0。也许我会,一旦这个问题得到解决,但它之前给了我一条错误消息。那时我刚刚返回x,这让我真的很不开心。
您省略了第二个(const)运算符实现中的尾随const
修饰符:
const float& Quaternion::operator[](const int &idx) const
{
// ...
}
相关文章:
- C++中的"expected unqualified-id"错误是什么?
- 错误:创建模板类时"expected unqualified-id before '{' token"
- C 和 C++:带有错误"Expected unqualified-id"的数据文件
- 为什么我总是"error: expected unqualified-id before 'case'"?
- C++ 文本消息解码器脚本 编译器 "expects unqualified id" 之前 if 和 else 语句
- "[Error] expected unqualified-Id before 'if'"这个代码块是什么意思
- "expected unqualified-id" 这就是它所说的一切
- 使用宏访问指向数据成员的指针会导致"error: expected unqualified-id before ‘*’ token"
- 库特上的Xcode "expected unqualified-id"
- 创建函数时接收"expected unqualified-id"
- 运算符 [] 重载的"error: expected unqualified-id before 'float' "
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- "expected unqualified-id before 'void'" & "string in class does not name a type" 错误
- "Expected unqualified-id"流
- C++ Unordered_Map:在编译时接收"error: expected unqualified-id before ‘[’ token"
- 尝试编译时出现错误"expected unqualified-id before ‘{’ token {"
- 调试前C++错误"expected unqualified-id '(' token"
- C++ 错误"expected an unqualified id before ')' token"(第 1 行)
- 通过指针访问类的成员时"unqualified-id"编译器错误
- Xcode说"Expected unqualified -id"