473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1447
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
3
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T>...
2
by: dave | last post by:
Why is this the same: (*(*pCurrent).pData).name as this: pCurrent->pData->name what is the benefit to the first? if any? why are the parenthesis important? thanks
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
9
by: seberino | last post by:
Is there any advantage to a language having a nice mathematically compact grammar like LISP does? (or at least used to?) Many have admired the mathematically simple grammar of LISP in which much...
2
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
4
by: casul | last post by:
Hi All, Given the following code that just defines a dummy class "my_class" and a dummy wrapper "my_wrapper" with a main program : #include <iostream> template< typename _Tag > class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.