469,946 Members | 2,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

having trouble with casting operators..

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::operator metric()
{
metric m;
return(m);
}

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

metric::operator english()
{
english e;
return(e);
}
Aug 20 '05 #1
7 1348
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);

Aug 20 '05 #2
JustSomeGuy wrote:
[...]
// 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
Aug 20 '05 #3

"Assertor" <gi******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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::operator(metric & m)
{
//... do stuff
return(e);
}
Aug 20 '05 #4
JustSomeGuy wrote:
"Assertor" <gi******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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::operator(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.
Aug 20 '05 #5
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::operator metric()
{
metric m;
return(m);
}

// metric.cpp
#include "metric.h"
*Add* the following line:

#include "english.h"
metric::operator 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
Aug 21 '05 #6
I'm wondering...
What is the difference between:

english::operator metric()
vs
metric & metric::operator=(english &e)

metric e;
english e;

m=e // is this going to call the casting operator or the assignment
operator?
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Q9********************@comcast.com...
JustSomeGuy wrote:
[...]
// 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

Aug 21 '05 #7
JustSomeGuy wrote:
I'm wondering...
What is the difference between:

english::operator metric()
vs
metric & metric::operator=(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.
[..]

Aug 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by JustSomeGuy | last post: by
3 posts views Thread by Andy Lomax | last post: by
2 posts views Thread by dave | last post: by
6 posts views Thread by Philipp Schumann | last post: by
18 posts views Thread by Marco | last post: by
2 posts views Thread by Giulio Petrucci | last post: by
4 posts views Thread by techie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.