By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,786 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

problem with STL container and dynamic classes

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
jo*******@yahoo.com wrote:
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
Jul 22 '05 #2

P: n/a
Thomas Maier-Komor <ma******@lpr.e-technik.no-spam.tu-muenchen.de> wrote in message news:<cl**********@wsc10.lrz-muenchen.de>...

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
Jul 22 '05 #3

P: n/a

<jo*******@yahoo.com> wrote in message
news:12**************************@posting.google.c om...
Thomas Maier-Komor <ma******@lpr.e-technik.no-spam.tu-muenchen.de> wrote
in message news:<cl**********@wsc10.lrz-muenchen.de>...

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.


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
Jul 22 '05 #4

P: n/a
On Fri, 22 Oct 2004 20:20:03 +0100, John Harrison wrote:

<jo*******@yahoo.com> wrote in message
news:12**************************@posting.google.c om...
Thomas Maier-Komor <ma******@lpr.e-technik.no-spam.tu-muenchen.de> wrote
in message news:<cl**********@wsc10.lrz-muenchen.de>...

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.


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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.