h******t 发帖数: 4 | 1 Could any C++ guru help me? Thanks a million in advance.
I need to write two classes each of which refers the other class. However, I
could not compile it. Below is a sample code to show what I really need. It
has two files, test.h and test.cc. The implementation is not given; however,
it could be compiled with "g++ -c test.cc",
/******* beginning of the source code of test.h *********/
#include
class index_object_1;
class index_object_2;
class index_object_1_comparator;
class index_object |
y****i 发帖数: 156 | 2 ///
// Change reference to pointer
#include
class index_object_1;
class index_object_2;
class index_object_2_comparator
{
public:
bool operator() (const index_object_2 * lhs, const index_object_2 * rhs)
const
{
return true;
}
};
class index_object_1_comparator {
public:
bool operator() (const index_object_1 * lhs, const index_object_1 * rhs)
const
{
return true;
}
};
class index_object_1 {
private:
double index;
std::set
【在 h******t 的大作中提到】 : Could any C++ guru help me? Thanks a million in advance. : I need to write two classes each of which refers the other class. However, I : could not compile it. Below is a sample code to show what I really need. It : has two files, test.h and test.cc. The implementation is not given; however, : it could be compiled with "g++ -c test.cc", : /******* beginning of the source code of test.h *********/ : #include : class index_object_1; : class index_object_2; : class index_object_1_comparator;
|
h******t 发帖数: 4 | 3 Thanks a lot. It certainly works. I would like to learn more from you. Why can
I not use reference? By using pointers, I have to maintain memory allocation
on my own. For example, if I have a set like
std::set s1;
Whenever I insert a pointer to an object of index_object_1 to the set, I have
to deallocate it when I finish with the object. Sometimes, it is hard to track
if I release all the memory allocated. Is the way you suggested the only way
to make
【在 y****i 的大作中提到】 : /// : // Change reference to pointer : #include : class index_object_1; : class index_object_2; : class index_object_2_comparator : { : public: : bool operator() (const index_object_2 * lhs, const index_object_2 * rhs) : const
|
y****i 发帖数: 156 | 4 According to my understanding:
when you declare a variable(class type), the compiler need its information
to allocate memory
class A
{
...
};
A a;
However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it
.
A *p;
This is the only way I know at present. So you have to manage memory by
youself.
track
way
Not hard. you retrieve the pointer to from the set, delete it whenever you
finished the use. |
c********e 发帖数: 383 | 5 all what u said is correct, another thing to be noted is that you can NOT
have a reference referencing nothing.
it
【在 y****i 的大作中提到】 : According to my understanding: : when you declare a variable(class type), the compiler need its information : to allocate memory : class A : { : ... : }; : A a; : However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it : .
|
h******t 发帖数: 4 | 6 Thanks a lot! Very good advice. Now I am thinking if I need to change my other
sets in my applicaiton to set to set to
make consistent. It is easy to get confused later on when I look at two
iterators, one is pointing to "some_object" and another one is pointing to "
some_object*".
it
【在 y****i 的大作中提到】 : According to my understanding: : when you declare a variable(class type), the compiler need its information : to allocate memory : class A : { : ... : }; : A a; : However, for a pointer, it will allocate only 4 byte(on 32-bit machine) for it : .
|
h******t 发帖数: 4 | 7 what do you mean? could you please elaborate a little bit more? Thanks!
【在 c********e 的大作中提到】 : all what u said is correct, another thing to be noted is that you can NOT : have a reference referencing nothing. : : it
|
y****i 发帖数: 156 | 8 When you declare a reference
int a= 3;
int &n = a; // must init it
【在 h******t 的大作中提到】 : what do you mean? could you please elaborate a little bit more? Thanks!
|
N***m 发帖数: 4460 | 9 I only change test.h to the following. It passed the compilation. But I do
not
know why?
test.h
=============================
#include
class index_object_1_comparator;
class index_object_2_comparator;
class index_object_2;
class index_object_2_comparator {
public:
bool operator() (const index_object_2 & lhs, const index_object_2 & rhs)
const;
};
class index_object_1 {
private:
double index;
std::set::iterator iter;
public:
index_ob |
d****p 发帖数: 685 | 10 Your code is perfect if you only swap the class declaration/definitons as
below:
index_obj_1 class forward declaration
index_obj_2 class forward declaration
index_obj_1_comparator definition
index_obj_2_comparator definition
index_obj_1 class definition
index_obj_2 class definition
No need to change reference to pointer because in nature they are identical. |
t****t 发帖数: 6806 | 11 You only need to know one thing:
STL container does not require the element type be complete at the time of
declaration, until you instantiate a member that requires an element to be
complete. However, it does require the comparator to be complete.
For example:
class A;
class A_comp;
class B {
std::vector va1; // OK
std::set sa1; // OK
std::vector va2(10); // error: initialize a vector with 10 elements but
the element is incomplete
std::vector va3(0); // error: although you |
N***m 发帖数: 4460 | 12 this is very helpful to green hands like me.
【在 t****t 的大作中提到】 : You only need to know one thing: : STL container does not require the element type be complete at the time of : declaration, until you instantiate a member that requires an element to be : complete. However, it does require the comparator to be complete. : For example: : class A; : class A_comp; : class B { : std::vector va1; // OK : std::set sa1; // OK
|