How do I get around circular references in C++?

Discussion in 'C++' started by michael, May 21, 2007.

  1. michael

    michael Guest

    Hi All,

    This is probably not the best example of this but it illustrates my point.
    What I have is:

    class Obj {
    private:
    string some;
    string stuff;
    public:
    set<Obj, CompareObjs> ObjSet;
    };

    class CompareObjs {
    public:
    bool operator ()(Obj& lhs, Obj& rhs){
    return true;
    }
    }

    so each of these classes has a reference to the other, and they appear in
    the same file.
    No matter what order they appear in one of them is not happy.
    Is it possible to get around this without having two files?

    Thanks for your help

    Michael
     
    michael, May 21, 2007
    #1
    1. Advertising

  2. On 21 Maj, 14:27, "michael" <> wrote:
    > Hi All,
    >
    > This is probably not the best example of this but it illustrates my point.
    > What I have is:
    >
    > class Obj {
    > private:
    > string some;
    > string stuff;
    > public:
    > set<Obj, CompareObjs> ObjSet;
    >
    > };
    >
    > class CompareObjs {
    > public:
    > bool operator ()(Obj& lhs, Obj& rhs){
    > return true;
    > }
    >
    > }
    >
    > so each of these classes has a reference to the other, and they appear in
    > the same file.
    > No matter what order they appear in one of them is not happy.
    > Is it possible to get around this without having two files?


    Separate the declaration and definition of CompareObjs::eek:perator() and
    make a forward declaration of Obj:

    class Obj; // Forward declaration

    class CompareObjs {
    public:
    bool operator ()(Obj& lhs, Obj& rhs);
    };

    class Obj {
    public:
    std::set<Obj, CompareObjs> ObjSet;
    };

    bool CompareObjs::eek:perator ()(Obj &lhs, Obj &rhs)
    {
    return lhs < rhs;
    }

    Notice that for this to compiler you need to add an operator< to Obj,
    but you can just redefine CompareObjs::eek:perator() to perform the
    comparison any way you want.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 21, 2007
    #2
    1. Advertising

  3. michael

    Hari Guest

    michael je napisao:
    > Hi All,
    >
    > This is probably not the best example of this but it illustrates my point.
    > What I have is:
    >
    > class Obj {
    > private:
    > string some;
    > string stuff;
    > public:
    > set<Obj, CompareObjs> ObjSet;
    > };
    >
    > class CompareObjs {
    > public:
    > bool operator ()(Obj& lhs, Obj& rhs){
    > return true;
    > }
    > }
    >
    > so each of these classes has a reference to the other, and they appear in
    > the same file.
    > No matter what order they appear in one of them is not happy.
    > Is it possible to get around this without having two files?
    >
    > Thanks for your help
    >
    > Michael


    class Obj;

    class CompareObjs {
    public:
    bool operator ()(Obj& lhs, Obj& rhs);
    };

    class Obj {
    private:
    string some;
    string stuff;
    public:
    set<Obj, CompareObjs> ObjSet;
    };

    // Than implement code

    bool CompareObjs::eek:perator ()(Obj& lhs, Obj& rhs)
    {
    return true;
    }

    Best,
    Zaharije Pasalic
     
    Hari, May 21, 2007
    #3
  4. On 21 May 2007 05:38:46 -0700, Erik Wikström wrote:
    > make a forward declaration of Obj:


    Actually, that is not enough.

    #include <set>
    class Obj;
    class Obj { public: std::set<Obj> objset; };

    This code, when compiled with g++ 4.1.2 with _GLIBCXX_CONCEPT_CHECKS enabled,
    gives the following errors:

    /.../boost_concept_check.h: In instantiation of '__gnu_cxx::_SGIAssignableConcept<Obj>':
    /.../stl_set.h:111: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:216: error: '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a' has incomplete type
    test2.cc:3: error: forward declaration of 'class Obj'
    /.../boost_concept_check.h: In instantiation of '__gnu_cxx::_BinaryFunctionConcept<std::less<Obj>, bool, Obj, Obj>':
    /.../stl_set.h:112: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:361: error: '__gnu_cxx::_BinaryFunctionConcept<_Func, _Return, _First, _Second>::__first' has incomplete type
    test2.cc:3: error: forward declaration of 'class Obj'
    /.../boost_concept_check.h:362: error: '__gnu_cxx::_BinaryFunctionConcept<_Func, _Return, _First, _Second>::__second' has incomplete type
    test2.cc:3: error: forward declaration of 'class Obj'
    /.../boost_concept_check.h: In member function 'void __gnu_cxx::_SGIAssignableConcept<_Tp>::__constraints() [with _Tp = Obj]':
    /.../stl_set.h:111: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:208: error: using invalid field '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
    /.../stl_set.h:111: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:209: error: using invalid field '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
    /.../boost_concept_check.h:209: error: using invalid field '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
    /.../stl_set.h:111: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:210: error: using invalid field '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
    /.../boost_concept_check.h: In member function 'void __gnu_cxx::_BinaryFunctionConcept<_Func, _Return, _First, _Second>::__constraints() [with _Func = std::less<Obj>, _Return = bool, _First = Obj, _Second = Obj]':
    /.../stl_set.h:112: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:358: error: using invalid field '__gnu_cxx::_BinaryFunctionConcept<_Func, _Return, _First, _Second>::__first'
    /.../boost_concept_check.h:358: error: using invalid field '__gnu_cxx::_BinaryFunctionConcept<_Func, _Return, _First, _Second>::__second'
    /.../boost_concept_check.h: In member function 'void __gnu_cxx::_SGIAssignableConcept<_Tp>::__const_constraints(const _Tp&) [with _Tp = Obj]':
    /.../boost_concept_check.h:210: instantiated from 'void __gnu_cxx::_SGIAssignableConcept<_Tp>::__constraints() [with _Tp = Obj]'
    /.../stl_set.h:111: instantiated from 'std::set<Obj, std::less<Obj>, std::allocator<Obj> >'
    test2.cc:3: instantiated from here
    /.../boost_concept_check.h:214: error: using invalid field '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'

    --
    Joel Yliluoma - http://bisqwit.iki.fi/
    : comprehension = 1 / (2 ^ precision)
     
    Joel Yliluoma, May 21, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan C Douglas

    Circular references are not supported

    Dan C Douglas, Aug 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,442
    Steve C. Orr, MCSD
    Aug 13, 2003
  2. Bill Jones

    Re: Circular References

    Bill Jones, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    =?Utf-8?B?RGF2ZQ==?=
    Apr 8, 2004
  3. =?Utf-8?B?S2VpdGggRi4=?=

    Circular References in Visual Studio

    =?Utf-8?B?S2VpdGggRi4=?=, Aug 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,401
    Cowboy \(Gregory A. Beamer\) [MVP]
    Aug 6, 2004
  4. BBuff
    Replies:
    0
    Views:
    2,478
    BBuff
    Sep 26, 2005
  5. Kiuhnm
    Replies:
    16
    Views:
    761
    Jonathan Mcdougall
    Jan 3, 2005
Loading...

Share This Page