Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with STL container and dynamic classes

joetekubi@yahoo.com
Guest
 
Posts: n/a
#1: Jul 22 '05
hello all,

while working with STL containers and various class
composition, I can across a bug that I can't seem
to find.
When using a STL container in a derived class with
composition, everything is good. If I use a pointer
to a base class, the container fails with a SIGSEGV.

TIA for any assistance.

Example code is below
Platform: Redhat 9, compile with "g++ -g test.cpp"

//===================== cut here ================================
#include <iostream>
#include <set> // STL set container for nexthop IP addresses

class Carbo
{
private:
std::set < int > qtest;
std::set < int >::iterator qiter; // destination address iterator
public:
Carbo();
~Carbo();
void DoSomething();
};

class bread
{
private:
Carbo *pcarbo;
public:
bread();
~bread();
void DoBread();
};

class potato
{
private:
Carbo mycarbo;
public:
potato();
~potato();
void DoPotato();
};

using namespace std;

Carbo::Carbo(){ cout << "Carbo ctor" << endl; }

Carbo::~Carbo() { cout << "Carbo dtor" << endl; }

void Carbo::DoSomething()
{
cout << "qtest before insert size = " << qtest.size() << endl;
qtest.insert(2);
qtest.insert(4);
qtest.insert(14);
qtest.insert(3);
cout << "qtest after insert size = " << qtest.size() << endl;
}

bread::bread()
{
cout << "bread ctor " << endl;
Carbo *pcarbo = new Carbo();
}

bread::~bread() { cout << " bread dtor" << endl; }

void bread::DoBread() { pcarbo->DoSomething(); }


potato::potato() : mycarbo() { cout << "potato ctor " << endl; }

potato::~potato() { cout << " potato dtor" << endl; }

void potato::DoPotato() { mycarbo.DoSomething(); }


int main()
{
potato sweetpotato;
sweetpotato.DoPotato(); // works ok, contained class

bread wholewheat;
wholewheat.DoBread(); // fails - pointer to class

return 0;
}

Thomas Maier-Komor
Guest
 
Posts: n/a
#2: Jul 22 '05

re: problem with STL container and dynamic classes


joetekubi@yahoo.com wrote:[color=blue]
> hello all,
>
> while working with STL containers and various class
> composition, I can across a bug that I can't seem
> to find.
> When using a STL container in a derived class with
> composition, everything is good. If I use a pointer
> to a base class, the container fails with a SIGSEGV.
>
> TIA for any assistance.
>
> Example code is below
> Platform: Redhat 9, compile with "g++ -g test.cpp"
>
> //===================== cut here ================================
> #include <iostream>
> #include <set> // STL set container for nexthop IP addresses
>
> class Carbo
> {
> private:
> std::set < int > qtest;
> std::set < int >::iterator qiter; // destination address iterator
> public:
> Carbo();
> ~Carbo();
> void DoSomething();
> };
>
> class bread
> {
> private:
> Carbo *pcarbo;
> public:
> bread();
> ~bread();
> void DoBread();
> };
>
> class potato
> {
> private:
> Carbo mycarbo;
> public:
> potato();
> ~potato();
> void DoPotato();
> };
>
> using namespace std;
>
> Carbo::Carbo(){ cout << "Carbo ctor" << endl; }
>
> Carbo::~Carbo() { cout << "Carbo dtor" << endl; }
>
> void Carbo::DoSomething()
> {
> cout << "qtest before insert size = " << qtest.size() << endl;
> qtest.insert(2);
> qtest.insert(4);
> qtest.insert(14);
> qtest.insert(3);
> cout << "qtest after insert size = " << qtest.size() << endl;
> }
>
> bread::bread()
> {
> cout << "bread ctor " << endl;
> Carbo *pcarbo = new Carbo();
> }
>
> bread::~bread() { cout << " bread dtor" << endl; }
>
> void bread::DoBread() { pcarbo->DoSomething(); }
>
>
> potato::potato() : mycarbo() { cout << "potato ctor " << endl; }
>
> potato::~potato() { cout << " potato dtor" << endl; }
>
> void potato::DoPotato() { mycarbo.DoSomething(); }
>
>
> int main()
> {
> potato sweetpotato;
> sweetpotato.DoPotato(); // works ok, contained class
>
> bread wholewheat;
> wholewheat.DoBread(); // fails - pointer to class
>
> return 0;
> }[/color]

Sun's CC gives the follwing warning which is actually also your error...

"m.cc", line 54: Warning: pcarbo hides bread::pcarbo.


Tom
joetekubi@yahoo.com
Guest
 
Posts: n/a
#3: Jul 22 '05

re: problem with STL container and dynamic classes


Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote in message news:<clb42d$81l$1@wsc10.lrz-muenchen.de>...
[color=blue]
>
> Sun's CC gives the follwing warning which is actually also your error...
>
> "m.cc", line 54: Warning: pcarbo hides bread::pcarbo.
>
>
> Tom[/color]

Sorry if I'm being dense here, but having a pointer inside a class
to another class is legal (right?), and the only way I can instantiate
the class is to " new class", so I can't see what the error is
and / or how I can work around it.

I've got plenty of C++ books, and several show this class form, but
none talk about any problems with it, except in the areas of
memory leaks, and destructors.

-joe
John Harrison
Guest
 
Posts: n/a
#4: Jul 22 '05

re: problem with STL container and dynamic classes



<joetekubi@yahoo.com> wrote in message
news:120ac8f5.0410221115.52db1f07@posting.google.c om...[color=blue]
> Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote
> in message news:<clb42d$81l$1@wsc10.lrz-muenchen.de>...
>[color=green]
>>
>> Sun's CC gives the follwing warning which is actually also your error...
>>
>> "m.cc", line 54: Warning: pcarbo hides bread::pcarbo.
>>
>>
>> Tom[/color]
>
> Sorry if I'm being dense here, but having a pointer inside a class
> to another class is legal (right?), and the only way I can instantiate
> the class is to " new class", so I can't see what the error is
> and / or how I can work around it.
>[/color]

The problem is very simple, you have declared pCarbo twice, the second
declaration hides the first.

First declaration

class bread
{
private:
Carbo *pcarbo;

Second declaration

bread::bread()
{
cout << "bread ctor " << endl;
Carbo *pcarbo = new Carbo();
}

I'm sure you didn't really mean the second declaration. Just replace with

bread::bread()
{
cout << "bread ctor " << endl;
pcarbo = new Carbo();
}

John


donno
Guest
 
Posts: n/a
#5: Jul 22 '05

re: problem with STL container and dynamic classes


On Fri, 22 Oct 2004 20:20:03 +0100, John Harrison wrote:
[color=blue]
>
> <joetekubi@yahoo.com> wrote in message
> news:120ac8f5.0410221115.52db1f07@posting.google.c om...[color=green]
>> Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote
>> in message news:<clb42d$81l$1@wsc10.lrz-muenchen.de>...
>>[color=darkred]
>>>
>>> Sun's CC gives the follwing warning which is actually also your error...
>>>
>>> "m.cc", line 54: Warning: pcarbo hides bread::pcarbo.
>>>
>>>
>>> Tom[/color]
>>
>> Sorry if I'm being dense here, but having a pointer inside a class
>> to another class is legal (right?), and the only way I can instantiate
>> the class is to " new class", so I can't see what the error is
>> and / or how I can work around it.
>>[/color]
>
> The problem is very simple, you have declared pCarbo twice, the second
> declaration hides the first.
>
> First declaration
>
> class bread
> {
> private:
> Carbo *pcarbo;
>
> Second declaration
>
> bread::bread()
> {
> cout << "bread ctor " << endl;
> Carbo *pcarbo = new Carbo();
> }
>
> I'm sure you didn't really mean the second declaration. Just replace with
>
> bread::bread()
> {
> cout << "bread ctor " << endl;
> pcarbo = new Carbo();
> }
>
> John[/color]

Guys,

Thanks muchos for being patient with me.

Your suggestions solved my problem!

-joe

Closed Thread