problem with STL container and dynamic classes

J

joetekubi

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;
}
 
T

Thomas Maier-Komor

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;
}

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

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


Tom
 
J

joetekubi

Thomas Maier-Komor said:
Sun's CC gives the follwing warning which is actually also your error...

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


Tom

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
 
J

John Harrison

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.

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
 
D

donno

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

Guys,

Thanks muchos for being patient with me.

Your suggestions solved my problem!

-joe
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top