开发者

C++ error - C2228, identifier not recognized as class

Here is the relevant code:

Canvas.cpp


    #ifndef CANVAS
    #define CANVAS

    #include "graphicsDatatypes.h"

    class Canvas
    {
    private:

        // Current and next points to draw to
        struct cartesianPoint currentPoint, nextPoint;

    public:

        Canvas::Canvas() { numLinesDrawn = 0; };
        Canvas::~Canvas();  

        struct cartesianPoint getCurrentPoint() { return currentPoint; };

        void setCurrentPoint(int x, int y)
        {
            currentPoint.x = x;
            currentPoint.y = y;
        }


    };

#endif

main.cpp


#include "glut-3.7.6-bin\glut.h"
#include "Canvas.cpp"

// Window size
int winWidth, winHeight;

// User's drawing space - current maximum of 4000 lines
Canvas userDrawSpace();

void callbackMouse(int button, int state, int x, int y)
{
    userDrawSpace.setCurrentPoint(x, y);
}

开发者_如何学C

The error I am getting is - error C2228: left of '.setCurrentPoint' must have class/struct/union

Any idea why? The class is pretty clearly defined, and include should simply be bringing in the text. Visual studios recognizes that Canvas is a class when I hover over it with my mouse, so I have no clue what's going on. Any help is appreciated, thanks.


The line

Canvas userDrawSpace();

looks like it should be creating Canvas object, but in fact it declares a function called userDrawSpace returning a Canvas object :-(.

That's a very common gotcha in C++.

Just get rid of the () and it should be ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜