预声明子类[重复]

问题描述 投票:0回答:1

这是我的情况:

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;

起初,但不起作用

c++ subclass declaration
1个回答
0
投票

您的原始代码段几乎是正确的,您只是忘记了在参数列表中限定bar

foo::bar operator+(const foo::bar & , int i){
   ...
}
© www.soinside.com 2019 - 2024. All rights reserved.