C++ class forward declaration

后端 未结 8 621
春和景丽
春和景丽 2020-12-03 01:52

When I try to compile this code i get:

52 C:\\Dev-Cpp\\Projektyyy\\strategy\\Tiles.h invalid use of undefined type `struct tile_tree_apple\' 
46 C:\\Dev-Cpp\         


        
8条回答
  •  生来不讨喜
    2020-12-03 02:24

    I had this:

    class paulzSprite;
    ...
    
    struct spriteFrame
    {
        spriteFrame(int, int, paulzSprite*, int, int);
        paulzSprite* pSprite; //points to the sprite class this struct frames
        static paulzSprite* pErase; //pointer to blanking sprite
        int x, y;
        int Xmin, Xmax, Ymin, Ymax; //limits, leave these to individual child classes, according to bitmap size
        bool move(int, int);
        bool DrawAt(int, int);
        bool dead;
    };
    
    spriteFrame::spriteFrame(int initx, int inity, paulzSprite* pSpr, int winWidth, int winHeight)
    {
        x = initx;
        y= inity;
        pSprite = pSpr;
        Xmin = Ymin = 0;
        Xmax = winWidth - pSpr->width;
        Ymax = winHeight - pSpr->height;
        dead = false;
    }
    

    ...

    Got the same grief as in the original question. Only solved by moving the definition of paulzSprite to after that of spriteFrame. Shouldn't the compiler be smarter than this (VC++, VS 11 Beta)?

    And btw, I wholeheartedly agree with Clifford's remark above "Pointers don't cause memory leaks, poor coding causes memory leaks". IMHO this is true of many other new "smart coding" features, which should not become a substitute for understanding what you are actually asking the computer to do.

提交回复
热议问题