Circular dependency - I think..

From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 07/26/04


Date: 25 Jul 2004 17:33:01 -0700

I think I'm caught up in a circular dependency problem. I'd thought
forward declaration was the solution to my problem but I'm sadly
mistaken (perphaps i've been in the lab toooo long today).
The header file for FOO is composed of BAR, likewise BAR is composed
of FOO.
If I complile FOO.cpp, Visual Studio's first complaint (C2146 syntax
error, missing ; before foo - the next complaint is BAR::FOO missing
storage type) points to the instantiation of FOO in BAR's header(i.e
the line FOO foo). Similarily if I compile BAR Visual first complaint
(C2146 syntax error, missing ; before bar - the next complaint is
FOO::BAR missing storage type) points to the instantiation of BAR in
FOO's header (i.e the line BAR bar).

The solution??

Thanks in advance.

The Code.

#ifndef FOO_H
#define FOO_H

# include "bar.h"
//class BAR;

class FOO
{
public:
  FOO();
  ~FOO();
  int GetFbk();
  void ComputeTorquerCmd();
private:
  int idx;
  void SetInUse();
  BAR bar;
};

#endif

# include "foo.h"
# include <iostream>

FOO::FOO() : idx(0)
{
  std::cout << " foo's constructor called " << std::endl;
}

FOO::~FOO()
{
  std::cout << " foo destructing " << std::endl;
}

int FOO::GetFbk()
{
 return ++idx; // for demo purposes
}

void FOO::ComputeTorquerCmd()
{
}

void FOO::SetInUse()
{
  bar.SetInUse();
  std::cout << " foo setting in use " << std::endl;
}
//////////////////////////////////////////

#ifndef BAR_H
#define BAR_H
# include "foo.h"

//class FOO;
class BAR
{
public:
  BAR();
  ~BAR();
  void GetPosFbkFoo();
  int GetInUse() const;
  void SetInUse();

private:
  int in_use;
  FOO foo;
};

#endif

# include "bar.h"
# include <iostream>

BAR::BAR() : in_use(0)
{
 std::cout << " bar's constructor " << std::endl;
}
BAR::~BAR() {}

void BAR::GetPosFbkFoo()
{
  SetInUse();
  if (in_use)
  {
    int jdx = foo.GetFbk();
    std::cout << jdx << std::endl;
  }
  else // do something else
  {
    foo.ComputeTorquerCmd();
  }
}

int BAR::GetInUse() const
{
  return in_use;
}

void BAR::SetInUse()
{
  in_use ^= 1;
}

An aside, I'm using google's newsreader which limits me from seeing a
response (used sparingly) momentarily. The point being, my follow ups
may be 'late'.

On the one hand my advisor tells me a sound design limits
'refractoring'. Later he tells me 'embrace change'. I'm confused.



Relevant Pages

  • Re: RISC OS modules with stock gcc?
    ... int bar; ... DCD &ff000004 ... IMPORT bar ... EXPORT foo ...
    (comp.sys.acorn.programmer)
  • Re: linking C++ functions in a C program
    ... contains one function named foo() which is compiled in C, ... Therefore, no matter how you classify bar(), this program ... void foo; ... guess the compiler can generate two references to foo, one adorned, ...
    (comp.lang.c)
  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: is assignment atomic/thread safe?
    ... void foo() ... with one thread running foo() while another runs bar(). ... and thus no guarantees are made about _when_ any given piece of code will execute. ...
    (comp.lang.java.programmer)
  • Re: Learning C, Trying to Understand & (unary op)
    ... void swap(int x, int y) ... int tmp; ... The formal parameters x and y are different objects from foo and bar; ...
    (comp.lang.c)