473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

problem with STL container and dynamic classes

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
4 1442
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
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Severin Ecker | last post by:
hi! normally i would simply do the following: std::vector<Element> vec; void somefunc() { Element e; vec.push_back(e); }
4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
2
by: Michael Olea | last post by:
Hiya. Frequently (as in at least twice now) I find that when I write some sort of container I want at least two out of three possible versions: o fixed max size known at compile time o fixed...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Bruin | last post by:
Hi All, I'm having a problem with MDI child forms when the reference to the MDI Parent is set in a Control library. (Sorry for the long post) I have an control library assembly which holds all...
20
by: Manuel | last post by:
Hi. Before all, please excuse me for bad english and for very newbie questions. I hope to don't boring you. I'm trying to write a very simple GUI using openGL. So I'm writing some different...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
5
by: Brad Baker | last post by:
I am trying to make a "tabbed" interface by iterating through a dataset with a conditional statement. For example: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.