R
Rui Maciel
I've been fooling around with a test class and meanwhile I stumbled on a
pointer problem.
I have two classes, one being nested in the other. I've wrote a method in
the nested class that would set a nested class's member pointer as a
pointer to an instance of the other class. Here is the code:
<code>
template <class tData>
class Foo
{
// snip
public:
class Bar
{
protected:
Foo<tData> *m_cursor;
public:
//snip
void point_to(Foo<tData> &);
};
};
// snip
template <class tData>
void Foo<tData>::Bar:
oint_to(Foo<tData> &pointed_to)
{
this->m_cursor = &pointed_to;
}
</code>
This code compiles under GCC without a single error or warning, even with
the -pedantic and -Wall flags.
The problem lies when I run the following test cod:
<code>
int main(int argc, char *argv[])
{
Foo<int> a;
Foo<int>::Bar b;
b.point_to( a);
return EXIT_SUCCESS;
}
</code>
That code also compiles withou a single problem. Yet, when I run the test
application, it crashes and the following message pops up:
<shell>
*** glibc detected *** free(): invalid pointer: 0xbfb01a10 ***
/bin/sh: line 1: 9703 Aborted ./mytree
Press Enter to continue!
</shell>
I can't see where the problem lies. Not only does the code compile but I
also the class points it's member pointer to an already declared variable.
I can't see what's going wrong in here.
So, can anyone shed some light on this problem?
Thanks in advance
Rui Maciel
pointer problem.
I have two classes, one being nested in the other. I've wrote a method in
the nested class that would set a nested class's member pointer as a
pointer to an instance of the other class. Here is the code:
<code>
template <class tData>
class Foo
{
// snip
public:
class Bar
{
protected:
Foo<tData> *m_cursor;
public:
//snip
void point_to(Foo<tData> &);
};
};
// snip
template <class tData>
void Foo<tData>::Bar:
{
this->m_cursor = &pointed_to;
}
</code>
This code compiles under GCC without a single error or warning, even with
the -pedantic and -Wall flags.
The problem lies when I run the following test cod:
<code>
int main(int argc, char *argv[])
{
Foo<int> a;
Foo<int>::Bar b;
b.point_to( a);
return EXIT_SUCCESS;
}
</code>
That code also compiles withou a single problem. Yet, when I run the test
application, it crashes and the following message pops up:
<shell>
*** glibc detected *** free(): invalid pointer: 0xbfb01a10 ***
/bin/sh: line 1: 9703 Aborted ./mytree
Press Enter to continue!
</shell>
I can't see where the problem lies. Not only does the code compile but I
also the class points it's member pointer to an already declared variable.
I can't see what's going wrong in here.
So, can anyone shed some light on this problem?
Thanks in advance
Rui Maciel