C++ 什么';我构造子类对象的方式有什么问题?

C++ 什么';我构造子类对象的方式有什么问题?,c++,class,inheritance,mfc,C++,Class,Inheritance,Mfc,下面是我的子类: class BackGround :public Object{ public: BackGround(int y){ character.Load(_T("wallpaper.png")); x = 0; this->y = y; direct = 0; width = 1200; height = 375; } }; 我的基类: class Object{ public: CImage character;

下面是我的子类:

class BackGround :public Object{
public:
BackGround(int y){
    character.Load(_T("wallpaper.png"));
    x = 0;
    this->y = y;
    direct = 0;
    width = 1200;
    height = 375;
}
};
我的基类:

class Object{
public:
CImage character;     
int x;             
int y;
int direct;       
int speed;
int width;      
int height;
int Xcenter;
int Ycenter;

Object(){}

void draw(HDC hDC){
    character.Draw(hDC, x, y, width, height, 0, direct*height,width, height);
}
};
当我创建类背景的对象时,
背景Bg1(0);
背景Bg2(-WINDOW_HEIGHT)

出现了以下错误:

1>  MainFrm.cpp
1>c:\users\desktop\mfcgame\mfcgame\childview.h(46): error C2059: syntax error: 'const'
1>c:\users\desktop\mfcgame\mfcgame\childview.h(47): error C2059: syntax error:“-”
1>  MFCGame.cpp
1>c:\users\desktop\mfcgame\mfcgame\childview.h(46): error C2059: syntax error: 'const'
1>c:\users\desktop\mfcgame\mfcgame\childview.h(47): error C2059: syntax error:“-”
1>  ChildView.cpp
1>c:\users\desktop\mfcgame\mfcgame\childview.h(46): error C2059: syntax error: 'const'
1>c:\users\desktop\mfcgame\mfcgame\childview.h(47): error C2059: syntax error:“-”
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(67): error C2228: left of '.draw' must have class/struct/union type
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(68): error C2228: left of '.draw' must have class/struct/union type
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(116): error C2228: left of '.y' must have class/struct/union type
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(118): error C2228: left of '.y' must have class/struct/union type
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(120): error C2228: left of '.y' must have class/struct/union type
1>c:\users\desktop\mfcgame\mfcgame\childview.cpp(122): error C2228: left of '.y' must have class/struct/union type
我想你有

class CChildView : public CWnd
{
    // ...
    BackGround Bg1(0);                // Line 46
    BackGround Bg2(-WINDOW_HEIGHT);   // Line 47
};
声明成员变量的语法不正确。
(编译器认为这些看起来像是成员函数的声明。)

如果您使用的是C++11,则可以编写

class CChildView : public CWnd
{
    // ...
    BackGround Bg1 {0}; 
    BackGround Bg2 {-WINDOW_HEIGHT};
};

使用卷轴,或者可以初始化构造函数的初始化列表中的成员,这些列表在所有C++版本中工作:

class CChildView : public CWnd
{
    CChildView();
    // ...
    BackGround Bg1; 
    BackGround Bg2;
};

// ... 

CChildView::CChildView() : Bg1(0), Bg2(-WINDOW_HEIGHT)
{
    // ...
}
我想你有

class CChildView : public CWnd
{
    // ...
    BackGround Bg1(0);                // Line 46
    BackGround Bg2(-WINDOW_HEIGHT);   // Line 47
};
声明成员变量的语法不正确。
(编译器认为这些看起来像是成员函数的声明。)

如果您使用的是C++11,则可以编写

class CChildView : public CWnd
{
    // ...
    BackGround Bg1 {0}; 
    BackGround Bg2 {-WINDOW_HEIGHT};
};

使用卷轴,或者可以初始化构造函数的初始化列表中的成员,这些列表在所有C++版本中工作:

class CChildView : public CWnd
{
    CChildView();
    // ...
    BackGround Bg1; 
    BackGround Bg2;
};

// ... 

CChildView::CChildView() : Bg1(0), Bg2(-WINDOW_HEIGHT)
{
    // ...
}

“错误来了”不是很有用的信息。请张贴准确的错误。(使用复制和粘贴。)从输出窗口而不是从错误列表窗口复制消息。(那里通常有更多更好的信息。)另外,
窗口高度的定义是什么?
窗口高度是一个宏。
定义窗口高度1400
错误指示背景Bg1(0);背景Bg2(-WINDOW_HEIGHT)儿童视图代码在哪里?“出现错误”不是很有用的信息。请张贴准确的错误。(使用复制和粘贴。)从输出窗口而不是从错误列表窗口复制消息。(那里通常有更多更好的信息。)另外,
窗口高度的定义是什么?
窗口高度是一个宏。
定义窗口高度1400
错误指示背景Bg1(0);背景Bg2(-WINDOW_HEIGHT)儿童视图代码在哪里?