Re: Constructor problem
From: Chiller (..._at_...)
Date: 04/10/04
- Next message: Cam: "Re: [OT] Re: Problem with release build of program using pointers"
- Previous message: Mark A. Gibbs: "Re: Constructor problem"
- In reply to: Chiller: "Constructor problem"
- Next in thread: John Harrison: "Re: Constructor problem"
- Reply: John Harrison: "Re: Constructor problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 10 Apr 2004 05:33:10 GMT
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
- Next message: Cam: "Re: [OT] Re: Problem with release build of program using pointers"
- Previous message: Mark A. Gibbs: "Re: Constructor problem"
- In reply to: Chiller: "Constructor problem"
- Next in thread: John Harrison: "Re: Constructor problem"
- Reply: John Harrison: "Re: Constructor problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|