M
ma740988
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.
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.