Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I get around circular references in C++?

Reply
Thread Tools

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

 
 
michael
Guest
Posts: n/a
 
      05-21-2007
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


 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-21-2007
On 21 Maj, 14:27, "michael" <(E-Mail Removed)> 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: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: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:perator() to perform the
comparison any way you want.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
Hari
Guest
Posts: n/a
 
      05-21-2007

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:perator ()(Obj& lhs, Obj& rhs)
{
return true;
}

Best,
Zaharije Pasalic

 
Reply With Quote
 
Joel Yliluoma
Guest
Posts: n/a
 
      05-21-2007
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>::__constrain ts() [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_con straints(const _Tp&) [with _Tp = Obj]':
/.../boost_concept_check.h:210: instantiated from 'void __gnu_cxx::_SGIAssignableConcept<_Tp>::__constrain ts() [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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
Circular References in Visual Studio =?Utf-8?B?S2VpdGggRi4=?= ASP .Net 3 08-06-2004 04:42 PM
Re: Circular References Bill Jones ASP .Net 1 04-08-2004 10:16 PM
Circular references are not supported Dan C Douglas ASP .Net 1 08-13-2003 09:21 PM



Advertisments