473,405 Members | 2,171 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,405 software developers and data experts.

Constructor problem

I am in the process of writing a class that will represent metric distances
by accepting a value (ie, 3) and a unit of measure (ie, m).

I've written my constructor in the .h file as

Distance (int, char);

I've written the constructor in the .cpp implementation file as:

Distance :: Distance ( int n, char m) : nu(n), me(m) {}

When I try to enter a value in main using:
Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared, yet the 5 is accepted.
Considering I declared these variables in the initial constructor I can't
understand why I'm getting this error.

I'd appreciate it if someone could give me an explanation of what I'm doing
wrong and how I can correct my mistake.

Thanks

Jul 22 '05 #1
28 1292
On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
Distance (int, char); Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared,


It is undeclared. m is a identifier, not a char literal as 'm' would
be.

Jul 22 '05 #2
On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
Distance (int, char); Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared,


It is undeclared. m is a identifier, not a char literal as 'm' would
be.

Jul 22 '05 #3
"Chiller" <...@...> wrote in message
news:3d******************************@news.teranew s.com...
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m).

I've written my constructor in the .h file as

Distance (int, char);

I've written the constructor in the .cpp implementation file as:

Distance :: Distance ( int n, char m) : nu(n), me(m) {}

When I try to enter a value in main using:
Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared, yet the 5 is accepted.
Considering I declared these variables in the initial constructor I can't
understand why I'm getting this error.

I'd appreciate it if someone could give me an explanation of what I'm doing wrong and how I can correct my mistake.


If you mean the literal character 'm', then you can do:

Distance a = Distance(5, 'm');

or, more concisely:

Distance a(5, 'm');

If you don't mean the literal character 'm', then you need to be more
specific about what it is you are trying to do. In particular, provide an
actual compilable piece of code - what you provided is not compilable as is.

Best regards,

Tom
Jul 22 '05 #4
"Chiller" <...@...> wrote in message
news:3d******************************@news.teranew s.com...
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m).

I've written my constructor in the .h file as

Distance (int, char);

I've written the constructor in the .cpp implementation file as:

Distance :: Distance ( int n, char m) : nu(n), me(m) {}

When I try to enter a value in main using:
Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared, yet the 5 is accepted.
Considering I declared these variables in the initial constructor I can't
understand why I'm getting this error.

I'd appreciate it if someone could give me an explanation of what I'm doing wrong and how I can correct my mistake.


If you mean the literal character 'm', then you can do:

Distance a = Distance(5, 'm');

or, more concisely:

Distance a(5, 'm');

If you don't mean the literal character 'm', then you need to be more
specific about what it is you are trying to do. In particular, provide an
actual compilable piece of code - what you provided is not compilable as is.

Best regards,

Tom
Jul 22 '05 #5
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?

Thanks
Jul 22 '05 #6
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?

Thanks
Jul 22 '05 #7
Chiller wrote:
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?


yes, but really, i'm still not sure what exactly you're looking for.

it *sounds* like you want to do something more like this:

class DistanceUnit
{
private:
DistanceUnit(int value) :
v_(value),
n_(name)
{}

DistanceUnit(DistanceUnit const&);
void operator=(DistanceUnit const&);

int v_;

friend class Distance;
};

class Distance
{
public:
static const DistanceUnit m;
static const DistanceUnit km;

Distance(int n, const DistanceUnit& unit) :
value_(n),
unit_(unit)
{}

private:
int value_;
DistanceUnit unit_;
};

Distance::DistanceUnit m(1);
Distance::DistanceUnit km(1000);

to use:

using Distance::m;
Distance a(5, m);

or:

Distance a(5, Distance::m);

this is just a *very* rough starting point - i wrote this off the cuff
without even checking anything (maybe you should put DistanceUnit in
Distance - that's up to you). unless you're willing to supply more
information, i can't offer you any more than this. but starting from
here you can create a system that can handle arbitrary distance units in
meters, kilometers, millimeters, inches, feet, nautical miles or cubits
(though you'd have to give up on the ints and go to floats at least),
can convert automatically between any two of them and (if you're clever
enough to add a string to DistanceUnit) print out the length in the
given unit (or any other chosen unit).

mark

Jul 22 '05 #8
Chiller wrote:
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?


yes, but really, i'm still not sure what exactly you're looking for.

it *sounds* like you want to do something more like this:

class DistanceUnit
{
private:
DistanceUnit(int value) :
v_(value),
n_(name)
{}

DistanceUnit(DistanceUnit const&);
void operator=(DistanceUnit const&);

int v_;

friend class Distance;
};

class Distance
{
public:
static const DistanceUnit m;
static const DistanceUnit km;

Distance(int n, const DistanceUnit& unit) :
value_(n),
unit_(unit)
{}

private:
int value_;
DistanceUnit unit_;
};

Distance::DistanceUnit m(1);
Distance::DistanceUnit km(1000);

to use:

using Distance::m;
Distance a(5, m);

or:

Distance a(5, Distance::m);

this is just a *very* rough starting point - i wrote this off the cuff
without even checking anything (maybe you should put DistanceUnit in
Distance - that's up to you). unless you're willing to supply more
information, i can't offer you any more than this. but starting from
here you can create a system that can handle arbitrary distance units in
meters, kilometers, millimeters, inches, feet, nautical miles or cubits
(though you'd have to give up on the ints and go to floats at least),
can convert automatically between any two of them and (if you're clever
enough to add a string to DistanceUnit) print out the length in the
given unit (or any other chosen unit).

mark

Jul 22 '05 #9
Below I have included the .h file .cpp implementation file. The .cpp file
has a TEST_DISTANCE driver that needs to be declared to the preprocessor to
allow it to be compiled.

I'd greatly appreciate some advice on what I'm doing wrong and how I can
correct the problem.

Whenever I compile I get the following errors:

Distance.cpp(32) : error C2556: 'char Distance::measure(void) const' :
overloaded function differs only by return type from 'int
Distance::measure(void) const'

Distance.h(25) : see declaration of 'Distance::measure'

Distance.cpp(32) : error C2371: 'Distance::measure' : redefinition;
different basic types

Distance.h(25) : see declaration of 'Distance::measure'

Distance.cpp(39) : error C2264: 'Distance::measure' : error in function
definition or declaration; function not called

..h file as follows

**************************************************

#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, char) ; // constructor - takes int and char values

Distance (int) ; // constructor - takes int value

Distance (void) ; // default - zero

//access member functions

int number (void) const;

int measure (void) const;
private :

int nu ; // the value

char me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif

..cpp file as follows

*****************************************

#include "Distance.h"

#include <iostream>

#include <string>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructor

Distance :: Distance (int n, char m) : nu(n), me(m) {}

Distance :: Distance (int n) : nu(n) {}

Distance :: Distance (void) : nu(0) {}

// access functions

int Distance :: number (void) const

{

return nu;

}

char Distance :: measure (void) const

{

return me;

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << "," << d.measure() << ")" ;

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6);

Distance b (4);

Distance c (2);

Distance d;

Distance e (5, 'm');
cout << a << endl << b << endl << c << endl << d << endl << e << endl;

cin.ignore();

return 0; // normal termination

}

#endif


Jul 22 '05 #10
Below I have included the .h file .cpp implementation file. The .cpp file
has a TEST_DISTANCE driver that needs to be declared to the preprocessor to
allow it to be compiled.

I'd greatly appreciate some advice on what I'm doing wrong and how I can
correct the problem.

Whenever I compile I get the following errors:

Distance.cpp(32) : error C2556: 'char Distance::measure(void) const' :
overloaded function differs only by return type from 'int
Distance::measure(void) const'

Distance.h(25) : see declaration of 'Distance::measure'

Distance.cpp(32) : error C2371: 'Distance::measure' : redefinition;
different basic types

Distance.h(25) : see declaration of 'Distance::measure'

Distance.cpp(39) : error C2264: 'Distance::measure' : error in function
definition or declaration; function not called

..h file as follows

**************************************************

#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, char) ; // constructor - takes int and char values

Distance (int) ; // constructor - takes int value

Distance (void) ; // default - zero

//access member functions

int number (void) const;

int measure (void) const;
private :

int nu ; // the value

char me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif

..cpp file as follows

*****************************************

#include "Distance.h"

#include <iostream>

#include <string>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructor

Distance :: Distance (int n, char m) : nu(n), me(m) {}

Distance :: Distance (int n) : nu(n) {}

Distance :: Distance (void) : nu(0) {}

// access functions

int Distance :: number (void) const

{

return nu;

}

char Distance :: measure (void) const

{

return me;

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << "," << d.measure() << ")" ;

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6);

Distance b (4);

Distance c (2);

Distance d;

Distance e (5, 'm');
cout << a << endl << b << endl << c << endl << d << endl << e << endl;

cin.ignore();

return 0; // normal termination

}

#endif


Jul 22 '05 #11

"Chiller" <...@...> wrote in message
news:59******************************@news.teranew s.com...
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

Its more common to write

Distance a(5, m);
Distance a(5, km);
without having to use ' ' or declaring m, km?

Thanks


Probably what you want is an enum

enum
{
m,
km,
miles,
};

etc.

Put that at the top of you program and then change char to int. I.e. you
constructor becomes

Distance (int, int);

But I'd seriously consider using longer names than m, km etc. How about

enum
{
units_meters,
units_kilometers,
units_miles,
};

that's how it is normally done.

john
Jul 22 '05 #12

"Chiller" <...@...> wrote in message
news:59******************************@news.teranew s.com...
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

Its more common to write

Distance a(5, m);
Distance a(5, km);
without having to use ' ' or declaring m, km?

Thanks


Probably what you want is an enum

enum
{
m,
km,
miles,
};

etc.

Put that at the top of you program and then change char to int. I.e. you
constructor becomes

Distance (int, int);

But I'd seriously consider using longer names than m, km etc. How about

enum
{
units_meters,
units_kilometers,
units_miles,
};

that's how it is normally done.

john
Jul 22 '05 #13

"Chiller" <...@...> wrote in message
news:2f******************************@news.teranew s.com...
Below I have included the .h file .cpp implementation file. The .cpp file
has a TEST_DISTANCE driver that needs to be declared to the preprocessor to allow it to be compiled.

I'd greatly appreciate some advice on what I'm doing wrong and how I can
correct the problem.

Whenever I compile I get the following errors:

Distance.cpp(32) : error C2556: 'char Distance::measure(void) const' :
overloaded function differs only by return type from 'int
Distance::measure(void) const'

[snip]
int measure (void) const;
You declare measure as returning an int here

[snip]
char Distance :: measure (void) const


But you define measure and returning a char here. The definition and
dclaration must be the same.

john
Jul 22 '05 #14

"Chiller" <...@...> wrote in message
news:2f******************************@news.teranew s.com...
Below I have included the .h file .cpp implementation file. The .cpp file
has a TEST_DISTANCE driver that needs to be declared to the preprocessor to allow it to be compiled.

I'd greatly appreciate some advice on what I'm doing wrong and how I can
correct the problem.

Whenever I compile I get the following errors:

Distance.cpp(32) : error C2556: 'char Distance::measure(void) const' :
overloaded function differs only by return type from 'int
Distance::measure(void) const'

[snip]
int measure (void) const;
You declare measure as returning an int here

[snip]
char Distance :: measure (void) const


But you define measure and returning a char here. The definition and
dclaration must be the same.

john
Jul 22 '05 #15
Thanks Mark,

I'm trying to write a class (Distance.h file and a Distance.cpp) file that
will accept two arguments, one being a value and the other being a unit (ie
km, m, or cm). I'm trying to write the class to include overloads for ==,
!=, <, <=, > and >= which will return a boolean value. As well as overloads
of + and -. Obviously the overloads will only work on multimple instances,
ie when I enter more than a single value.

What I've written so far is included within this thread.

Thanks
Jul 22 '05 #16
Thanks Mark,

I'm trying to write a class (Distance.h file and a Distance.cpp) file that
will accept two arguments, one being a value and the other being a unit (ie
km, m, or cm). I'm trying to write the class to include overloads for ==,
!=, <, <=, > and >= which will return a boolean value. As well as overloads
of + and -. Obviously the overloads will only work on multimple instances,
ie when I enter more than a single value.

What I've written so far is included within this thread.

Thanks
Jul 22 '05 #17
Thanks John,

I obviously need to take a break for a while.

Regards
Jul 22 '05 #18
Thanks John,

I obviously need to take a break for a while.

Regards
Jul 22 '05 #19
John,

I've taken your suggestion and included enum; however, when I output the
stored values to screen, all the values are int values. What I'm trying to
do is to be able to enter a value and unit and be able to output the same
value and unit. ie, if I input Distance a (5, m) I'd like to be able to
output the same. Once I've got this working my next step will be to
implement some overloaded operators to perform addition, subtraction etc on
any objects created by the class.

I've included what I've done so far below. If you compile and execute the
program you'll see what I mean by the output being in the wrong format.I'd
appreciate any advice on this.

Thanks
Distance.h

************************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, int) ; // constructor - takes int values

Distance (int) ; // constructor - takes int value

Distance (void) ; // default - zero

//access member functions

int number (void) const;

int measure (void) const;
private :

int nu ; // the value

int me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif

Distance.cpp as follows:

*****************************************

#include "Distance.h"

#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructor

Distance :: Distance (int n, int m) : nu(n), me(m) {}

Distance :: Distance (int n) : nu(n) {}

Distance :: Distance (void) : nu(0) {}

enum

{

m,

km,

};

// access functions

int Distance :: number (void) const

{

return nu;

}

int Distance :: measure (void) const

{

return me;

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << "," << d.measure() << ")" ;

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6);

Distance b (4);

Distance c (2);

Distance d;

Distance e (5, m);
cout << a << endl << b << endl << c << endl << d << endl << e << endl;

cin.ignore();

return 0; // normal termination

}

#endif

Jul 22 '05 #20
John,

I've taken your suggestion and included enum; however, when I output the
stored values to screen, all the values are int values. What I'm trying to
do is to be able to enter a value and unit and be able to output the same
value and unit. ie, if I input Distance a (5, m) I'd like to be able to
output the same. Once I've got this working my next step will be to
implement some overloaded operators to perform addition, subtraction etc on
any objects created by the class.

I've included what I've done so far below. If you compile and execute the
program you'll see what I mean by the output being in the wrong format.I'd
appreciate any advice on this.

Thanks
Distance.h

************************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, int) ; // constructor - takes int values

Distance (int) ; // constructor - takes int value

Distance (void) ; // default - zero

//access member functions

int number (void) const;

int measure (void) const;
private :

int nu ; // the value

int me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif

Distance.cpp as follows:

*****************************************

#include "Distance.h"

#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructor

Distance :: Distance (int n, int m) : nu(n), me(m) {}

Distance :: Distance (int n) : nu(n) {}

Distance :: Distance (void) : nu(0) {}

enum

{

m,

km,

};

// access functions

int Distance :: number (void) const

{

return nu;

}

int Distance :: measure (void) const

{

return me;

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << "," << d.measure() << ")" ;

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6);

Distance b (4);

Distance c (2);

Distance d;

Distance e (5, m);
cout << a << endl << b << endl << c << endl << d << endl << e << endl;

cin.ignore();

return 0; // normal termination

}

#endif

Jul 22 '05 #21

"Chiller" <...@...> wrote in message
news:59******************************@news.teranew s.com...
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?

Thanks

You need to refine your knowledge of constants.
The arguements provided are not variables. 5 is a constant ( a literal
value). The second arguement can be a std::string or enum but its also a
constant here. You don't have to use the second arguement if you plan on
providing a consistant measurement base (like meters). Personally, i'ld
prefer a constructor with an init list and passing constants by reference:

#include <iostream>
#include <string>

class Distance
{
public:
Distance(const int& r_distance) : meters(r_distance) { }
virtual ~Distance() { }
void display() const { std::cout << "distance = " << meters <<
std::endl; }
private:
int meters;
};

int main()
{
Distance dist(5000);
dist.display();
return 0;
}

Jul 22 '05 #22

"Chiller" <...@...> wrote in message
news:59******************************@news.teranew s.com...
Is there a way that I can write the constructor so that it will simply
accept two variables, ie. when I create an object I can do so by simply
typing

Distance a = Distance (5, m); or
Distance a = Distance (5, km); etc

without having to use ' ' or declaring m, km?

Thanks

You need to refine your knowledge of constants.
The arguements provided are not variables. 5 is a constant ( a literal
value). The second arguement can be a std::string or enum but its also a
constant here. You don't have to use the second arguement if you plan on
providing a consistant measurement base (like meters). Personally, i'ld
prefer a constructor with an init list and passing constants by reference:

#include <iostream>
#include <string>

class Distance
{
public:
Distance(const int& r_distance) : meters(r_distance) { }
virtual ~Distance() { }
void display() const { std::cout << "distance = " << meters <<
std::endl; }
private:
int meters;
};

int main()
{
Distance dist(5000);
dist.display();
return 0;
}

Jul 22 '05 #23

"Chiller" <...@...> wrote in message
news:63******************************@news.teranew s.com...
John,

I've taken your suggestion and included enum; however, when I output the
stored values to screen, all the values are int values.
That's because enum's are integer constants.
What I'm trying to
do is to be able to enter a value and unit and be able to output the same
value and unit. ie, if I input Distance a (5, m) I'd like to be able to
output the same. Once I've got this working my next step will be to
implement some overloaded operators to perform addition, subtraction etc on any objects created by the class.


There's no way to do that other than the hard way

enum { m, km, };

switch (d.measure())
{
case m:
out << "m";
break;
case km:
out << "km";
break;
}

There's various ways of tarting this up, for instance you could have an
array of unit strings.

enum { m, km, };

const char* const unit_str[] = { "m", "km" };

out << unit_str[d.measure()];

but essentially you have to test the int value of your units and print out
the appropriate string. There is no way of automatically converting the name
of something in your program to a string.

john
Jul 22 '05 #24

"Chiller" <...@...> wrote in message
news:63******************************@news.teranew s.com...
John,

I've taken your suggestion and included enum; however, when I output the
stored values to screen, all the values are int values.
That's because enum's are integer constants.
What I'm trying to
do is to be able to enter a value and unit and be able to output the same
value and unit. ie, if I input Distance a (5, m) I'd like to be able to
output the same. Once I've got this working my next step will be to
implement some overloaded operators to perform addition, subtraction etc on any objects created by the class.


There's no way to do that other than the hard way

enum { m, km, };

switch (d.measure())
{
case m:
out << "m";
break;
case km:
out << "km";
break;
}

There's various ways of tarting this up, for instance you could have an
array of unit strings.

enum { m, km, };

const char* const unit_str[] = { "m", "km" };

out << unit_str[d.measure()];

but essentially you have to test the int value of your units and print out
the appropriate string. There is no way of automatically converting the name
of something in your program to a string.

john
Jul 22 '05 #25
>enum
{
m,
km,
miles,
};

etc.

Put that at the top of you program and then change char to int. I.e. you
constructor becomes

Distance (int, int);


I am curious about your suggestion to change the second parameter from a char
to an int.

Wouldn't it be better to have the enumeration typed and then use that type as
the second parameter? Then the author of the class need not worry that he has
been passed an invalid indicator of unit as the compiler will allow only values
specified in the enumeration to be passed. Of course the caller could cast any
value to the enumerated type, but then they get what they deserve.

Regards.
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #26
>enum
{
m,
km,
miles,
};

etc.

Put that at the top of you program and then change char to int. I.e. you
constructor becomes

Distance (int, int);


I am curious about your suggestion to change the second parameter from a char
to an int.

Wouldn't it be better to have the enumeration typed and then use that type as
the second parameter? Then the author of the class need not worry that he has
been passed an invalid indicator of unit as the compiler will allow only values
specified in the enumeration to be passed. Of course the caller could cast any
value to the enumerated type, but then they get what they deserve.

Regards.
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #27
> >

I am curious about your suggestion to change the second parameter from a char to an int.

Wouldn't it be better to have the enumeration typed and then use that type as the second parameter? Then the author of the class need not worry that he has been passed an invalid indicator of unit as the compiler will allow only values specified in the enumeration to be passed. Of course the caller could cast any value to the enumerated type, but then they get what they deserve.


Maybe, but I was just trying to introduce one new concept at a time.

Incidentally the OP took my advice but if you read his latest post he's
changed back from an int to a char for some spurious reason.

john
Jul 22 '05 #28
> >

I am curious about your suggestion to change the second parameter from a char to an int.

Wouldn't it be better to have the enumeration typed and then use that type as the second parameter? Then the author of the class need not worry that he has been passed an invalid indicator of unit as the compiler will allow only values specified in the enumeration to be passed. Of course the caller could cast any value to the enumerated type, but then they get what they deserve.


Maybe, but I was just trying to introduce one new concept at a time.

Incidentally the OP took my advice but if you read his latest post he's
changed back from an int to a char for some spurious reason.

john
Jul 22 '05 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.