简体   繁体   中英

error C2146: syntax error : missing ';' before identifier 'm_ball' C++, MFC

I have beloved error C2146. I checked for possible errors and I'm unable to find one( as far as I can see there are all needed ; and after rightclick on Ball->GoToDefinition it correctly shows class declaration)

BallMasterDoc.h

#pragma once
class CBallMasterDoc : public CDocument
{
private:
    Ball m_ball; //syntax error : missing ';' before identifier 'm_ball'
    Pod m_pod; //syntax error : missing ';' before identifier 'm_pod'

BallMasterDoc.cpp

#include "Pod.h"
#include "Ball.h"
#include "BallMasterDoc.h"

Ball.h

#pragma once

const COLORREF BLUE = RGB(0, 0, 255);
extern int g_iRadius, g_iHeight;
extern int g_iWidth, g_iMaxWidth;//pod...

class Ball
{
public:
    Ball();
    ~Ball();
    BOOL Move(CPoint podPosition);
    BOOL Start(){ return m_bStart; }
    BOOL Collision(){ return m_bCollision; }
    BOOL End(){ return m_bEnd; }
    CRect GetArea();
private:
    BOOL CheckCollision(CPoint podPosition);
    float m_fDirection;
    int m_iB; // y = ax + B
    BOOL m_bUpDown;//true - up
    BOOL m_bStart;
    BOOL m_bCollision;
    BOOL m_bEnd;
    CPoint m_ballCentre;
    CPoint m_collisionPoint;
};

Pod.h

#pragma once
const COLORREF BLACK = RGB(0, 0, 0);
extern int g_iWidth, g_iMaxWidth;
class Pod
{
public:
    Pod();
    ~Pod();
    BOOL MoveLeft();
    BOOL MoveRight();
    CPoint Position() { return m_Middle; }
private:
    CPoint m_Middle;
};

Please tell me what's wrong.

EDIT

All includes are in cpp files(those generated by wizard and mine) I show just these 3 cause rest is imo uninvolved in this case. I'm learning MFC from magic book called: Microsoft Visual C++ Windows Applications by Example and there all includes go to cpp files(even if I feel its strange and book is far from best wizard supports this style...)

好的,我将#include"Ball.h"/"Pod.h"放在BallMasterDoc.h中 ,它解决了这个问题,但这不能满足答案,只要魔术书曾经提供过有效的代码即可(当然,这不是确切的程序,对我来说,大部分该代码仍然无法正常工作),但现在已经没有关系了

Since BallMasterDoc.h depends on knowing about the Ball and Pod classes, that header file should include Ball.h and Pod.h -- instead of relying on whichever .cpp included BallMasterDoc.h to also include those other headers.

The BallMasterDoc.cpp that you posted looks OK, so your errors likely come from another .cpp file which does include BallMasterDoc.h but did not include the headers required by that header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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