Sign in to follow this  
Mawr

Circular Dependence in header files - C++

Recommended Posts

I'm having a problem using two classes that require each other in their definitions. I'm not sure how to solve it in a way that would preserve functionality that I'm intending. mainclass.h
#ifndef GUARD_MAINCLASS_H
#define GUARD_MAINCLASS_H

#include <vector>
#include "button.h"

using namespace std;

class mainClass
{
public: 
  float xres;
  float yres;
  float xpixel;
  float ypixel;
  
  vector<button> btnList;
  
  float TransXpt(float p);
  float TransYpt(float p);
  
  mainClass(float XRES, float YRES);
  void addButton(button btn);
};
#endif

mainclass.cpp
#ifndef GUARD_MAINCLASS_CPP
#define GUARD_MAINCLASS_CPP

#include <vector>
#include "mainclass.h"
#include "button.h"

using namespace std;
void mainClass::addButton(button btn)
{
  btnList.push_back(btn);
}
  
mainClass::mainClass(float XRES, float YRES)
: xres(XRES), yres(YRES), xpixel(2/xres), ypixel(2/yres)
{
}

float mainClass::TransXpt(float p)
{
  return ((p-(xres/2))*xpixel);
}

float mainClass::TransYpt(float p)
{
  return ((p-(yres/2))*ypixel);
}
#endif

button.h
#ifndef GUARD_BUTTON_H
#define GUARD_BUTTON_H

class button
{
public:
  float x,y,xSize, ySize;
  mainClass *mainPtr;
  button(float X, float Y, float XSIZE, float YSIZE, mainClass *PTR);
  void draw();
  
};
#endif

button.cpp
#ifndef GUARD_BUTTON_CPP
#define GUARD_BUTTON_CPP

#include <gl/gl.h>
#include "button.h"
#include "mainclass.h"

button::button(float X, float Y, float XSIZE, float YSIZE, mainClass *PTR)
: x(X), y(Y), xSize(XSIZE), ySize(YSIZE)
{
  mainPtr=PTR;
}

void button::draw()
{

  glBegin(GL_POLYGON);
  glColor3f(1,1,1);
  glVertex2f(mainPtr->TransXpt(x), mainPtr->TransYpt(y));
  glVertex2f(mainPtr->TransXpt(x+xSize), mainPtr->TransYpt(y));
  glVertex2f(mainPtr->TransXpt(x+xSize), mainPtr->TransYpt(y+ySize));
  glVertex2f(mainPtr->TransXpt(x), mainPtr->TransYpt(y+ySize));
  glEnd();
}
#endif

Ideally, I want to be able to pass a pointer of my mainclass into other classes, so they don't have to know the perspective information on how to draw on the screen, yet have access to the mainClass, which does. If this works out, I plan on expanding to all redundent information based functions. If a point needs clarifying, just ask, I'll try to respond as quickly as possible. Thank you for your time.

Share this post


Link to post
Share on other sites
I tried adding a line(class mainClass;) to button.h before I included mainclass.h, and added a line(class button;) to mainclass.h likewise. It failed linking, said undefined references to the gl functions in button.cpp. Not sure if it's related or not, will continue testing.

Share this post


Link to post
Share on other sites
Ok, it wasn't related, so your fix worked. Thanks! But I'm curious as to whether I'll have to 'forward' the sub-classes in the header files for classes I want to pass the main-class, or is there a more elegent solution?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this