С++ Зависимость классов друг от друга
Я еще только изучаю С++ и решил сделать свой проект графического интерфейса с нуля (с использованием SDL2) и пока делал классы столкнулся с проблемой: в общем виде , класс А использует метод класса В, при этом класс В использует методы класса А.
Вопрос, а как делать так, чтобы оно скомпилировалось? Как обойти эту проблему? Forward def помогает только в случае указателей, а у меня именно вызов функций. P.S. надеюсь не ошибся с оформлением поста
ButtonClasses.h:
#ifndef BUTTONCLASSES_H
#define BUTTONCLASSES_H
#include <SDL.h>
//#include "viewPort.h"
//#include "menu.h"
struct menuMachine;
struct viewPort;
struct menu;
menu* currentmenu;
struct B_WARP : public button
{
menu* nextM=nullptr;
menuMachine* myMenuMachine;
void activate() override;
void draw(SDL_Renderer*) override;
void target(SDL_Renderer*) override;
~B_WARP();
};
struct B_HSCROLLER : public button
{
int leftbound, rightbound;
SDL_Rect background;
SDL_Color backcolor;
SDL_Rect arrowL, arrowR;
SDL_Color arrowcolor[2];
void activate() override;
void draw(SDL_Renderer*) override;
void target(SDL_Renderer*) override;
};
#endif // BUTTONCLASSES_H
ButtonClasses.cpp:
#include "buttonClasses.h"
#include "buttonFlist.h"
extern SDL_Point mouse, mouseL;
extern int dx, dy;
void B_WARP::activate()
{
if(nextM!=nullptr)
{
// menuMachine* cmenu=(menuMachine*)object;
currentmenu=nextM;
}
}
void B_WARP::draw(SDL_Renderer* nren)
{
SDL_SetRenderDrawColorExt(nren,currst);
SDL_RenderDrawRect(nren,&butt);
}
void B_WARP::target(SDL_Renderer* nren)
{
SDL_Color tempSt=currst;
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&butt);
}
B_WARP:: ~B_WARP()
{
if(nextM!=nullptr)
delete [] nextM;
}
void B_HSCROLLER::activate()
{
int i;
//check speed!!
//да да, return гораздо удлобнее,чем флаговая переменная и куча проверок
if(SDL_PointInRect(&mouse,&arrowL))
{
butt.x-=2;
if(butt.x<leftbound)
butt.x+=2;
return;
}
if(SDL_PointInRect(&mouse,&arrowR))
{
butt.x+=2;
if((butt.x+butt.w)>rightbound)
butt.x-=2;
return;
}
butt.x+=dx;
if((butt.x<leftbound)||(butt.x+butt.w)>rightbound)
butt.x-=dx;
else
{
for(i=0;i<vp->buttamount;i++)
{
vp->pads[i].butt.x=dx*(vp->hscrollspeed);
}
}
}
void B_HSCROLLER::draw(SDL_Renderer* nren)
{
SDL_SetRenderDrawColorExt(nren,backcolor);
SDL_RenderDrawRect(nren,&background);
SDL_SetRenderDrawColorExt(nren,currst);
SDL_RenderDrawRect(nren,&butt);
SDL_SetRenderDrawColorExt(nren,arrowcolor[0]);
SDL_RenderDrawRect(nren,&arrowL);
SDL_SetRenderDrawColorExt(nren,arrowcolor[1]);
SDL_RenderDrawRect(nren,&arrowR);
}
void B_HSCROLLER::target(SDL_Renderer* nren)
{
SDL_Color tempSt;
if(SDL_PointInRect(&mouse,&butt))
{
tempSt=currst;
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&butt);
}
if(SDL_PointInRect(&mouse,&arrowL))
{
tempSt=arrowcolor[0];
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&arrowL);
}
if(SDL_PointInRect(&mouse,&arrowR))
{
tempSt=arrowcolor[1];
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&arrowR);
}
}
void B_VSCROLLER::activate()
{
int i;
if(SDL_PointInRect(&mouse,&arrowDn))
{
butt.y+=vp->vscrollspeed;
if((butt.y+butt.h)<dnbound)
butt.y-=vp->vscrollspeed;
return;
}
if(SDL_PointInRect(&mouse,&arrowUp))
{
butt.y-=vp->hscrollspeed;
if((butt.y)>upbound)
butt.y+=vp->hscrollspeed;
return;
}
butt.y+=dy;
if((butt.y<upbound)||(butt.y+butt.h)>dnbound)
butt.y-=dy;
else
{
for(i=0;i<vp->buttamount;i++)
{
vp->pads[i].butt.y=dy*(vp->vscrollspeed);
}
}
}
void B_VSCROLLER::draw(SDL_Renderer* nren)
{
SDL_SetRenderDrawColorExt(nren,backcolor);
SDL_RenderDrawRect(nren,&background);
SDL_SetRenderDrawColorExt(nren,currst);
SDL_RenderDrawRect(nren,&butt);
SDL_SetRenderDrawColorExt(nren,arrowcolor[0]);
SDL_RenderDrawRect(nren,&arrowUp);
SDL_SetRenderDrawColorExt(nren,arrowcolor[1]);
SDL_RenderDrawRect(nren,&arrowDn);
}
void B_VSCROLLER::target(SDL_Renderer* nren)
{
SDL_Color tempSt;
if(SDL_PointInRect(&mouse,&butt))
{
tempSt=currst;
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&butt);
}
if(SDL_PointInRect(&mouse,&arrowDn))
{
tempSt=arrowcolor[0];
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&arrowDn);
}
if(SDL_PointInRect(&mouse,&arrowUp))
{
tempSt=arrowcolor[1];
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&arrowUp);
}
}
void B_SLIDER::activate()
{
butt.x+=dx;
if((butt.x<leftbound)||(butt.x+butt.w)>rightbound)
butt.x-=dx;
else
if(callBack!=nullptr)
if(callBack(value, object))
error=true;
}
void B_SLIDER::draw(SDL_Renderer* nren)
{
SDL_SetRenderDrawColorExt(nren,backcolor);
SDL_RenderDrawRect(nren,&background);
SDL_SetRenderDrawColorExt(nren,currst);
SDL_RenderDrawRect(nren,&butt);
}
void B_SLIDER::target(SDL_Renderer* nren)
{
SDL_Color tempSt;
tempSt=currst;
tempSt.a=200;
SDL_SetRenderDrawColorExt(nren,tempSt);
SDL_RenderDrawRect(nren,&butt);
}
ViewPort.h:
#ifndef VIEWPORT_H_INCLUDED
#define VIEWPORT_H_INCLUDED
#include "buttonClasses.h"
//struct viewPort;
struct viewPort
{
SDL_Rect viewPort;
int borderW;
SDL_Color bordercolor;
SDL_Color wallpaper;
int buttamount;
button* pads=nullptr;
button* scrollerh=nullptr;
button* scrollerv=nullptr;
int hscrollspeed, vscrollspeed;
// viewPort();
int subVamount=0;
// viewPort* subV=nullptr;
void draw();
void activate(bool clicked);
void scrollupd();
~viewPort();
};
#endif // VIEWPORT_H_INCLUDED
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Функции
- не должны принимать другой класс иначе как через указатель или ссылку;
- предварительное объявление, +
- Определение тел функций вне класса, после объявлений.
Все. Например (с переполнением стека, но лень было писать много :))
#include <iostream>
#include <iomanip>
using namespace std;
struct A;
struct B
{
void out(const A& a) const;
};
struct A
{
void out(const B& b) const;
};
void B::out(const A& a) const
{
cout << "b";
a.out(*this);
};
void A::out(const B& b) const
{
cout << "a";
b.out(*this);
};
int main(int argc, char * argv[])
{
A a; B b;
a.out(b);
}