简体   繁体   English

C ++错误-C2228,标识符无法识别为类

[英]C++ error - C2228, identifier not recognized as class

Here is the relevant code: 以下是相关代码:

Canvas.cpp 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 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);
}

The error I am getting is - error C2228: left of '.setCurrentPoint' must have class/struct/union 我收到的错误是-错误C2228:“。setCurrentPoint”的左侧必须具有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. 当我用鼠标悬停在Canvas上时,Visual Studios意识到Canvas是一类,所以我不知道发生了什么。 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 :-(. 看起来应该创建Canvas对象,但实际上它声明了一个名为userDrawSpace的函数,该函数返回Canvas对象:-(。

That's a very common gotcha in C++. 这是C ++中非常常见的陷阱。

Just get rid of the () and it should be ok. 只需摆脱(),就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM