这是我的情况:
class foo{
public:
class bar{
friend bar operator+(const bar & , int i);
}
};
foo::bar operator+(const foo::bar & , int i){
...
}
如果我对此进行编译,则会得到一个错误Undefined symbols for architecture x86_64...
,所以我所做的是这个:
foo::bar operator+(const bar & , int i);
class foo{
public:
class bar{
friend bar operator+(const bar & , int i);
}
};
foo::bar operator+(const foo::bar & , int i){
...
}
但是现在是不知道什么是foo::bar
的声明,所以我添加了这个:
class foo{
public:
class bar;
}
foo::bar operator+(const bar & , int i);
class foo{
public:
class bar{
friend bar operator+(const bar & , int i);
}
};
foo::bar operator+(const foo::bar & , int i){
...
}
但是现在告诉我我正在重新定义类foo
,我该如何解决呢?
也尝试过
class foo::bar;
起初,但不起作用
您的原始代码段几乎是正确的,您只是忘记了在参数列表中限定bar
:
foo::bar operator+(const foo::bar & , int i){
...
}