为什么这个代码无法编译?

3
我已经在这里创建了这个类:

// 整数矩形类

class AguiRectangle {
    int x;
    int y;
    int width;
    int height;
public:

    bool isEmpty {

        return x == 0 && y == 0 &&
           width == 0 && height == 0;
    }

    int getTop() {
        return x;
    }
    int getLeft() {
        return y;
    }

    int getBottom() {
        return y + height;
    }
    int getRight() {
        return x + width;
    }

    AguiPoint getTopLeft()
    {
        return AguiPoint(getTop(),getLeft());
    }
    AguiPoint getBottomRight()
    {
        return AguiPoint(this->getBottom(),this->getRight());
    }
};

编译器告诉我 x、y、width 和 height 等变量未声明,还有其他的问题。几乎感觉这个类没有看到自己。

谢谢。

Error   14  error C2673: 'getBottomRight' : global functions do not have 'this' pointers    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   178
Error   16  error C2673: 'getBottomRight' : global functions do not have 'this' pointers    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   178
Error   13  error C2665: 'AguiPoint::AguiPoint' : none of the 4 overloads could convert all the argument types  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   174
Error   6   error C2628: 'AguiRectangle' followed by 'int' is illegal (did you forget a ';'?)   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   158
Error   3   error C2473: 'isEmpty' : looks like a function definition, but there is no parameter list.  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   153
Error   5   error C2238: unexpected token(s) preceding ';'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   156
Error   17  error C2227: left of '->getRight' must point to class/struct/union/generic type c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   178
Error   15  error C2227: left of '->getBottom' must point to class/struct/union/generic type    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   178
Error   2   error C2146: syntax error : missing ';' before identifier 'result'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   64
Error   19  error C2143: syntax error : missing ';' before '}'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   180
Error   21  error C2079: 'pp' uses undefined class 'AguiPointf' c:\Users\Josh\Documents\Visual Studio 2008\Projects\Agui\Alleg_5\main.cpp   35
Error   8   error C2065: 'y' : undeclared identifier    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   162
Error   9   error C2065: 'y' : undeclared identifier    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   166
Error   7   error C2065: 'x' : undeclared identifier    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   159
Error   11  error C2065: 'x' : undeclared identifier    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   169
Error   12  error C2065: 'width' : undeclared identifier    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   169
Error   10  error C2065: 'height' : undeclared identifier   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   166
Error   4   error C2059: syntax error : 'return'    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   155
Error   18  error C2059: syntax error : '}' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   180
Error   20  error C2059: syntax error : '}' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   180
Error   1   error C2027: use of undefined type 'AguiPointf' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\Agui\AguiBaseTypes.h   59

请提供更多的上下文信息,就我所看到的代码而言是正确的。 - WolfgangP
“among other things”? 比如什么? - Etienne de Martel
也许你的编译器需要一个 publicprivateprotected 属性。通常,类具有默认存储 private,结构体具有默认存储 public - Benoit
尽管编译器说了很多,但它也说“错误 3 错误 C2473:'isEmpty':看起来像是函数定义,但没有参数列表。” :) - liaK
3个回答

10

应该是

bool isEmpty()

替代

bool isEmpty

7
你应该使用 bool isEmpty() { ... } 吗?

3

first error I could find is

bool isEmpty {

应该变成

bool isEmpty() const {

在这个上下文中,“const”是用来做什么的? - jmasterx
一个类的const方法禁止修改成员变量。如果该方法进行了修改或调用了非const方法,将会导致编译错误。 - user459770
你可以调用常量的const方法(你不能调用常量的非const方法)。 - panzi
这是一个关于const正确性的问题,用于指定哪些方法不修改对象的状态。这对于避免一些愚蠢的错误非常有帮助,并且可以让编译器在编译时进行更多的检查。请参阅http://en.wikipedia.org/wiki/Const-correctness。 - jdehaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接