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

M

michael

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
H

Hari

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
 
J

Joel Yliluoma

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'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top