C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using Inheritance with static data???

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
David Goodyear
Guest





PostPosted: Tue Jan 27, 2004 10:59 am    Post subject: Using Inheritance with static data??? Reply with quote



Hi all, plz plz help this has been annoying me for way too long now Sad
(Please excuse any wrong terms)

I am trying to wrap various data types into specific object wrappers derived
from a base class.

The idea is to provide a generic print function in the super? class, that
they are all derived from. As they would all use the same formatting params,

I have put some static ints in the super? class. In the end I would derive
more than one class.

I cant use other approaches as the final code, will be something based on
this but with other objects, but currently would suffer from the problems
discovered here :(

In borland builder 5 the constructor for IntegerWrapper compiles with the
error param::Label is not an unambiguous base class of 'IntegerParameter'.

Could someone please explain the general flaw with the following code, and
maybe a solution to it?

Many thanks, for any and all help Smile - P.S This is not a school, college
etc... project... I have a mini project and am doing this to learn C++ many
thanks.

David

THIS WOULD BE THE HEADER FILE -> "CustomChildren.h"

#ifndef WRAP_TEST
#define WRAP_TEST

// String manipulation lib
#include <string>
// Output Libs
#include <ostream.h>
#include <iomanip.h>

class wrapper
{
public:
...
static void applyOutputFormat(const unsigned int Precision = 6,
const unsigned int _preLabelTabs = 0,
const unsigned int _PreValueTabs = 1,
const unsigned int _PreUnitTabs = 1,
const unsigned int _LabelFieldSpaces = 0,
const unsigned int _ValueFieldSpaces = 0,
const unsigned int _UnitFieldSpaces = 0);
friend ostream& operator<< (ostream& out, const wrapper& w); //
Creates a formatted string
...
protected:
char* Label, Units;
private:
static int PreValueTabs, PreUnitTabs, PreLabelTabs,
LabelFieldSpaces, ValueFieldSpaces, UnitFieldSpaces;
};

#ENDIF

class IntegerWrapper : public wrapper
{
public:
...
IntegerWrapper(const int _IntValue, const char* _Label, const char*
_Units): IntValue(_IntValue), Label(_Label) {}
...
protected:
private:
int IntValue;
};

THIS WOULD BE THE CPP FILE

#include
#include <iomanip.h>
#include "CustomChildren.h"

ostream& operator<< (ostream& out, const wrapper& w) {
int loopCntr;

for (loopCntr = 0; loopCntr < parameter::PreLabelTabs; loopCntr++) out <<
't';
out << setfill(' ') << setw(parameter::LabelFieldSpaces) << w.Label;

for (loopCntr = 0; loopCntr < parameter::PreValueTabs; loopCntr++) out <<
't';
out << setfill(' ') << setw(parameter::ValueFieldSpaces) <<
"VAL!";//Accessor for internal variable will go here.

for (loopCntr = 0; loopCntr < parameter::PreUnitTabs; loopCntr++) out <<
't';
out << setfill(' ') << setw(parameter::UnitFieldSpaces) << w.Units;

return out;
}

int main()
{
return 0;
}


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.567 / Virus Database: 358 - Release Date: 24/01/2004


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Mang
Guest





PostPosted: Tue Jan 27, 2004 6:40 pm    Post subject: Re: Using Inheritance with static data??? Reply with quote



Quote:


class IntegerWrapper : public wrapper
{
public:
...
IntegerWrapper(const int _IntValue, const char* _Label, const char*
_Units): IntValue(_IntValue), Label(_Label) {}
...
protected:
private:
int IntValue;
};

Well, the error message you got is actually quite precise.

The compiler (and me too) has no ideal what Label is - it is neither a base
class of IntegerWrapper, nor a member one could initialize - however you try to
initialize it in the initializer list.


regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Wed Jan 28, 2004 2:27 pm    Post subject: Re: Using Inheritance with static data??? Reply with quote



On 27 Jan 2004 05:59:30 -0500, "David Goodyear" <dag6 (AT) kent (DOT) ac.uk>
wrote in comp.lang.c++.moderated:

[snip]

Quote:
class wrapper
{
public:
...
static void applyOutputFormat(const unsigned int Precision = 6,
const unsigned int _preLabelTabs = 0,
const unsigned int _PreValueTabs = 1,
const unsigned int _PreUnitTabs = 1,
const unsigned int _LabelFieldSpaces = 0,
const unsigned int _ValueFieldSpaces = 0,
const unsigned int _UnitFieldSpaces = 0);

Among your other problems, your code is illegal under the C++
standard. All symbols beginning with an underscore followed by an
upper case letter, or containing two consecutive underscores anywhere
within them, are reserved for the compiler in all contexts. It is
invalid for you to define identifiers like _PreUnitTabs in your code.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dylan Nicholson
Guest





PostPosted: Wed Jan 28, 2004 2:28 pm    Post subject: Re: Using Inheritance with static data??? Reply with quote

Thomas Mang <a9804814 (AT) unet (DOT) univie.ac.at> wrote

Quote:


class IntegerWrapper : public wrapper
{
public:
...
IntegerWrapper(const int _IntValue, const char* _Label, const char*
_Units): IntValue(_IntValue), Label(_Label) {}
...
protected:
private:
int IntValue;
};

Well, the error message you got is actually quite precise.

The compiler (and me too) has no ideal what Label is - it is neither a base
class of IntegerWrapper, nor a member one could initialize - however you try to
initialize it in the initializer list.

Having been bitten by the same problem myself even after 10 years of

C++ programming, I sympathize with the OP's plight.
"Label" is a member of the base class, and C++ doesn't allow you to
initialize those in the initializer list of a derived class.

Probably the best solution (based on available information) is to make
Label private and allow initializing it through wrapper's constructor.
Making data members protected puts unnecessary constraints on the base
class - if you later wanted to change it to use std::string (a good
idea, btw), you would have to change all your derived classes.

Dylan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.