Что не так с моим способом создания объекта подкласса?

Вот мой подкласс:

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);
}
};

Когда я создаю объекты класса BackGround,
BackGround Bg1(0);
BackGround 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

0

Решение

Я думаю, что у вас есть

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)
{
// ...
}
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector