having trouble with casting operators..

J

JustSomeGuy

I have two classes, class english and class metric.
I seem to be having dificulty getting this to compile properly...
I'm using powerpc-apple-darwin8-g++-4.0.0

There are 5 files, main.cpp english.cpp english.h metric.h and metric.cpp

They really don't do anything in this example except demonstrate the
structure of my application, and hopefully the compiler errors I'm
experiencing.

error: expected type-specifier before 'english' metric.h:21


// metric.h
#ifndef METRIC_H
#define METRIC_H

#include "english.h"

class metric
{
private:
float kilometersperhour;
public:
operator english();
};
#endif

// english.h
#ifndef ENGLISH_H
#define ENGLISH_H

#include "metric.h"
class english
{
private:
float milesperhour;
public:
operator metric();
};
#endif

// english.cpp
#include "english.h"

english::eek:perator metric()
{
metric m;
return(m);
}

// metric.cpp
#include "metric.h"

metric::eek:perator english()
{
english e;
return(e);
}
 
A

Assertor

operator english(); <--- operator can not be recognized by compiler.
:) as u know.

As u know, operator redeifintion is as follows,
<return type> operator<operator what you want to redefine>(<input
parameter type> <input variable name>);
e.g. metric operator+(metric& other);
 
V

Victor Bazarov

JustSomeGuy said:
[...]
// metric.h
#ifndef METRIC_H
#define METRIC_H

#include "english.h"

class metric
{
private:
float kilometersperhour;
public:
operator english();
};
#endif

// english.h
#ifndef ENGLISH_H
#define ENGLISH_H

#include "metric.h"
class english
{
private:
float milesperhour;
public:
operator metric();
};
#endif

You have two files that include each other. The one you include first
will define its include guard and will not be included again when you
include the other one in it. Which means one of the classes will be
left undefined when the other class is compiled. Depending on the order
in which they will be included either 'operator metric' in 'english'
will be invalid (using undefined class 'metric') or vice versa. You
could try forward-declaing each class in the opposite header. Or you
could try declaring the functions

operator class metric(); // or operator class english();

If nothing works, you will need to redesign. For example, let your
class have operator metric*() (a pointer instead of an object) or
operator metric const& () (a reference to const).

V
 
J

JustSomeGuy

Assertor said:
operator english(); <--- operator can not be recognized by compiler.
:) as u know.

As u know, operator redeifintion is as follows,
<return type> operator<operator what you want to redefine>(<input
parameter type> <input variable name>);
e.g. metric operator+(metric& other);


so the syntax in the metric.h is:

english operator(metric & m);

and in the metric.cpp

english metric::eek:perator(metric & m)
{
//... do stuff
return(e);
}
 
K

Kyle

JustSomeGuy said:
so the syntax in the metric.h is:

english operator(metric & m);

and in the metric.cpp

english metric::eek:perator(metric & m)
{
//... do stuff
return(e);
}

errm ... no .. his (OP) syntax is actually good as he declare implict
type convertion operators, OP got a problem with headers including each
other and guarding themselves from beeing included more than once.
 
S

Serge Paccalin

Le samedi 20 août 2005 à 18:47:21, JustSomeGuy a écrit dans
comp.lang.c++ :
I have two classes, class english and class metric.
I seem to be having dificulty getting this to compile properly...
I'm using powerpc-apple-darwin8-g++-4.0.0

There are 5 files, main.cpp english.cpp english.h metric.h and metric.cpp

They really don't do anything in this example except demonstrate the
structure of my application, and hopefully the compiler errors I'm
experiencing.

error: expected type-specifier before 'english' metric.h:21

First of all, ignore what Assertor wrote.
// metric.h
#ifndef METRIC_H
#define METRIC_H

#include "english.h"

*Replace* the line above with:

class english;
class metric
{
private:
float kilometersperhour;
public:
operator english();
};
#endif

// english.h
#ifndef ENGLISH_H
#define ENGLISH_H

#include "metric.h"

*Replace* the line above with:

class metric;
class english
{
private:
float milesperhour;
public:
operator metric();
};
#endif

// english.cpp
#include "english.h"

*Add* the following line:

#include "metric.h"
english::eek:perator metric()
{
metric m;
return(m);
}

// metric.cpp
#include "metric.h"

*Add* the following line:

#include "english.h"
metric::eek:perator english()
{
english e;
return(e);
}


--
___________ 21/08/2005 10:29:19
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
J

JustSomeGuy

I'm wondering...
What is the difference between:

english::eek:perator metric()
vs
metric & metric::eek:perator=(english &e)

metric e;
english e;

m=e // is this going to call the casting operator or the assignment
operator?


Victor Bazarov said:
JustSomeGuy said:
[...]
// metric.h
#ifndef METRIC_H
#define METRIC_H

#include "english.h"

class metric
{
private:
float kilometersperhour;
public:
operator english();
};
#endif

// english.h
#ifndef ENGLISH_H
#define ENGLISH_H

#include "metric.h"
class english
{
private:
float milesperhour;
public:
operator metric();
};
#endif

You have two files that include each other. The one you include first
will define its include guard and will not be included again when you
include the other one in it. Which means one of the classes will be
left undefined when the other class is compiled. Depending on the order
in which they will be included either 'operator metric' in 'english'
will be invalid (using undefined class 'metric') or vice versa. You
could try forward-declaing each class in the opposite header. Or you
could try declaring the functions

operator class metric(); // or operator class english();

If nothing works, you will need to redesign. For example, let your
class have operator metric*() (a pointer instead of an object) or
operator metric const& () (a reference to const).

V
 
V

Victor Bazarov

JustSomeGuy said:
I'm wondering...
What is the difference between:

english::eek:perator metric()
vs
metric & metric::eek:perator=(english &e)

metric e;
english e;

m=e // is this going to call the casting operator or the assignment
operator?

(a) Don't top-post.

(b) Most likely you'll have a case of ambiguity on your hands
and the compiler will be the first to point it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top