我是Swift的新手,并且正在尝试编写一个符合Equatable的私有枚举.以下是我的代码的简化表示:
class Baz {
/* Other members in class Baz */
private enum Test: Equatable {
case Foo
case Bar
}
private func == (lhs: Test, rhs: Test) -> Bool {
//comparison
}
}
Run Code Online (Sandbox Code Playgroud)
在"=="方法的行上,编译器抱怨"操作符只允许在全局范围内".当我将enum Test和"=="方法更改为public时,将"=="移出类,然后错误消失.
我的问题是为私有枚举实现"=="方法的正确方法是什么?
任何帮助表示赞赏.
========
编辑:
谢谢大家帮帮我.我没有指定上面的私有枚举和函数在类中..(代码已更新)
我有一个类Foo,其构造函数是这样写的
Foo::Foo(std::vector<std::pair<int, char>> &Data)
: //Initialization list
{
//Some other initialization
}
Run Code Online (Sandbox Code Playgroud)
我尝试在我的代码中调用它
Foo(std::vector<std::pair<int, char>>
{
{10, 'a'}
});
Run Code Online (Sandbox Code Playgroud)
然后编译器给我一个 C4239 说
nonstandard extension used: 'argument': conversion from
'std::vector<std::pair<int,char>,std::allocator<_Ty>>' to
'std::vector<std::pair<int,char>,std::allocator<_Ty>> &'
Run Code Online (Sandbox Code Playgroud)
我理解该消息,但为什么编译器对这种转换不满意?
提前致谢。