Error: expected unqualified-id before 'bool'

Error: expected unqualified-id before 'bool'

我是初学者,正在尝试这个 link 中的最后一个 c++ 练习题:http://www.cplusplus.com/forum/articles/12974/ 这叫做毕业。 开始编写代码后,我决定将一个 class 定义分离到一个 header 文件中。 (如果我第一次定义自己的 header 文件,请记住这一点。曾经)我试图在 main.cpp 中定义 header 文件 class 的成员函数, 但 returns 一个错误。

这是给出错误的运算符函数定义:

//in main.cpp
Bunny::bool operator>(const Bunny &comparison) //ERROR!!! expected unqualified-id before 'bool'
{
    if (badbunny == false && comparison->badbunny == true)
        return true;
    if (badbunny == true && comparison->badbunny == false)
        return false;
    else if (badbunny == comparison->badbunny) {
    //......blah..blah..blah....nothing special here
}

这里是 header class 定义:

//in Bunny class.h
class Bunny
{
public:
    const char * name;
    const char * color;
    int age;
    char sex;
    bool badbunny;
    Bunny *next;
    //default constructor
    Bunny();
    Bunny(char * M_color);
    //operators
    bool operator<(const Bunny &comparison);
    bool operator>(const Bunny &comparison);
};

但是,当我直接输入 class 定义(以及 header 文件中我没有在此处输入的其他一些声明)时,请直接输入 main.cpp 并且不要打扰对于任何 header 文件,我的编译器都不会给我任何错误。 可以看到,"bool operator>(const Bunny &comparison);"在header文件中明确声明了。为什么我不能从 main.cpp 访问它? 我清楚地在 main.cpp 中 #include "Bunny class.h",并且 header 文件有一个守卫和一切。

确实是你函数声明的问题:

Bunny::bool operator>( ... )

正确的做法是:

bool Bunny::operator>( ... )

Bunny::属于函数名而不是return类型。