473,805 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1994
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.te ranews.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.te ranews.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(in t value) :
v_(value),
n_(name)
{}

DistanceUnit(Di stanceUnit const&);
void operator=(Dista nceUnit 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::Dista nceUnit m(1);
Distance::Dista nceUnit 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(in t value) :
v_(value),
n_(name)
{}

DistanceUnit(Di stanceUnit const&);
void operator=(Dista nceUnit 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::Dista nceUnit m(1);
Distance::Dista nceUnit 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::measu re(void) const' :
overloaded function differs only by return type from 'int
Distance::measu re(void) const'

Distance.h(25) : see declaration of 'Distance::meas ure'

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

Distance.h(25) : see declaration of 'Distance::meas ure'

Distance.cpp(39 ) : error C2264: 'Distance::meas ure' : 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

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

Similar topics

0
4258
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 (with no responces associated) :
11
2203
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 developers to call empty constructors. But the runtime sends me an exception when I try to serialize this class asking me to provide it with an
6
2043
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
3583
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 one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
45
6370
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
23
7240
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
16042
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
22
3632
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 v);
13
3980
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 is much appreciated. JD
9
23777
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 of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.