Re: Constructor problem

From: Chiller (..._at_...)
Date: 04/10/04


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



Relevant Pages

  • Re: Vigenere Cipher
    ... The fact that stats() is in Polish and that you haven't ... This declaration as good as nothing. ... > void deszyfruj(); ... Better spell it out: int main. ...
    (comp.lang.c)
  • Re: wits end
    ... The keyword "void" is none of these. ... arguments, and then returned a value of type "int *", one had to ... However, ANSI C had to permit the old-style non-prototype declaration, ... number of arguments, so hope the programmer gets it right", how ...
    (comp.lang.c)
  • Re: Confusion in ANSI Cs function concepts
    ... is no function declaration corresponding to the function call and the ... should assume a declaration with an int return type. ... the compiler will assume that fun is a function returning int ... in the above case the return type is mentioned as void *. ...
    (comp.lang.c)
  • Re: Function declaration (was: copy an istringstream)
    ... > nothing and returning int as parameter. ... > are ignored (as is in the case in our first parameter), ... Unfortunately C++ doesn't require void in function declaration. ... It seems that void foo2, int) is more intuitive declaration. ...
    (comp.lang.cpp)
  • Re: C Strings not returning from a function
    ... And as soon as malloc() is declared as returning int, ... new declaration is used, the behaviour is undefined. ... int main (void) ...
    (comp.lang.c)

Loading