I want to move impletation into cpp file, but I am stuck here.
wish your help.


I have 2 class A, and B.

#include "stdafx.h"

class A;
//class B
//{
//public:
// A* a;
// B();
// void Bdosomething();
//};
#include "b.h" // just the above content

//class A
//{
//public:
// B* b;
// A(B* bv);
//
// void Adosomething();
//};
#include "a.h" // just the above content

A::A(B* bv):b(bv){}
inline void A::Adosomething()
{
b->Bdosomething();
}
// move the above para to a.cpp file

B::B(){a = NULL;}
inline void B::Bdosomething()
{
if(!a)
{
a = new A(this);
a->Adosomething();
printf("a set to dosomething\n");
}
}
// move the above para to b.cpp file

int _tmain(int argc, _TCHAR* argv[])
{
B* pB = new B();
pB->Bdosomething();
return 0;
}

this works well, but when I move B's impletation into b.cpp
file, it complaints:
circref error LNK2019: unresolved external symbol "public: void __thiscall
B::Bdosomething(void)" (?Bdosomething@B@@QAEXXZ) referenced in function
_main
circref error LNK2019: unresolved external symbol "public: __thiscall
B::B(void)" (??0B@@QAE@XZ) referenced in function _main
circref fatal error LNK1120: 2 unresolved externals

thanks in advance.

Re: class circular reference problem by John

John
Wed Jun 23 03:08:49 CDT 2004

"asdfasd" <asdfasd@asdfasdf.com> wrote in message
news:exV9lMNWEHA.212@TK2MSFTNGP11.phx.gbl
> I want to move impletation into cpp file, but I am stuck here.
> wish your help.
>
>
> I have 2 class A, and B.
>
> #include "stdafx.h"
>
> class A;
> //class B
> //{
> //public:
> // A* a;
> // B();
> // void Bdosomething();
> //};
> #include "b.h" // just the above content
>
> //class A
> //{
> //public:
> // B* b;
> // A(B* bv);
> //
> // void Adosomething();
> //};
> #include "a.h" // just the above content
>
> A::A(B* bv):b(bv){}
> inline void A::Adosomething()
> {
> b->Bdosomething();
> }
> // move the above para to a.cpp file
>
> B::B(){a = NULL;}
> inline void B::Bdosomething()
> {
> if(!a)
> {
> a = new A(this);
> a->Adosomething();
> printf("a set to dosomething\n");
> }
> }
> // move the above para to b.cpp file
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> B* pB = new B();
> pB->Bdosomething();
> return 0;
> }
>
> this works well, but when I move B's impletation into b.cpp
> file, it complaints:
> circref error LNK2019: unresolved external symbol "public: void
> __thiscall B::Bdosomething(void)" (?Bdosomething@B@@QAEXXZ)
> referenced in function _main
> circref error LNK2019: unresolved external symbol "public: __thiscall
> B::B(void)" (??0B@@QAE@XZ) referenced in function _main
> circref fatal error LNK1120: 2 unresolved externals
>
> thanks in advance.

inline functions must be defined in the translation unit in which they are
called; they cannot be linked in from another translation unit.

In practice, this generally means defining them in a header file
(alternatives include defining them in another file that the header file
#includes --- call it, say, b.inl where "inl" stands for "inline" --- or in
the .cpp file that uses the function).


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Re: class circular reference problem by asdfasd

asdfasd
Thu Jun 24 04:02:02 CDT 2004

Hi, John:

seems there is no way except avoiding it?



"John Carson" <donaldquixote@datafast.net.au> wrote in message
news:eGRMrmPWEHA.2844@TK2MSFTNGP11.phx.gbl...
> "asdfasd" <asdfasd@asdfasdf.com> wrote in message
> news:exV9lMNWEHA.212@TK2MSFTNGP11.phx.gbl
> > I want to move impletation into cpp file, but I am stuck here.
> > wish your help.
> >
> >
> > I have 2 class A, and B.
> >
> > #include "stdafx.h"
> >
> > class A;
> > //class B
> > //{
> > //public:
> > // A* a;
> > // B();
> > // void Bdosomething();
> > //};
> > #include "b.h" // just the above content
> >
> > //class A
> > //{
> > //public:
> > // B* b;
> > // A(B* bv);
> > //
> > // void Adosomething();
> > //};
> > #include "a.h" // just the above content
> >
> > A::A(B* bv):b(bv){}
> > inline void A::Adosomething()
> > {
> > b->Bdosomething();
> > }
> > // move the above para to a.cpp file
> >
> > B::B(){a = NULL;}
> > inline void B::Bdosomething()
> > {
> > if(!a)
> > {
> > a = new A(this);
> > a->Adosomething();
> > printf("a set to dosomething\n");
> > }
> > }
> > // move the above para to b.cpp file
> >
> > int _tmain(int argc, _TCHAR* argv[])
> > {
> > B* pB = new B();
> > pB->Bdosomething();
> > return 0;
> > }
> >
> > this works well, but when I move B's impletation into b.cpp
> > file, it complaints:
> > circref error LNK2019: unresolved external symbol "public: void
> > __thiscall B::Bdosomething(void)" (?Bdosomething@B@@QAEXXZ)
> > referenced in function _main
> > circref error LNK2019: unresolved external symbol "public: __thiscall
> > B::B(void)" (??0B@@QAE@XZ) referenced in function _main
> > circref fatal error LNK1120: 2 unresolved externals
> >
> > thanks in advance.
>
> inline functions must be defined in the translation unit in which they are
> called; they cannot be linked in from another translation unit.
>
> In practice, this generally means defining them in a header file
> (alternatives include defining them in another file that the header file
> #includes --- call it, say, b.inl where "inl" stands for "inline" --- or
in
> the .cpp file that uses the function).
>
>
> --
> John Carson
> 1. To reply to email address, remove donald
> 2. Don't reply to email address (post here instead)
>