Seperate file, type conversion
Jul 8, 2012 at 6:54pm UTC
Hello everyone,
I have two file, interface, and implementation.
Here's the interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#ifndef CARTESIAN_H
#define DTIME_H
using namespace std;
class Cartesian{
public :
Cartesian();
Cartesian(int a, int b);
friend istream& operator >>(istream& in,Cartesian& c);
friend ostream& operator <<(ostream& out,Cartesian& c);
int input(char * c);
Cartesian operator +(Cartesian& c);
Cartesian operator -(Cartesian& c);
bool operator ==(Cartesian& c);
operator Polar();
private :
int x, y;
};
#endif
Here's the implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include "cartesian.h"
#include "polar.h"
using namespace std;
const int MAX=20;
Cartesian::Cartesian(){}
Cartesian::Cartesian(int a, int b):x(a),y(b){}
int Cartesian::input(char * c){
for (int i=0;i<strlen(c);i++){
if (!isdigit(*(c+i))){
cout<<"Invalid input!" ;
exit(1);
}
}
return atoi(c);
}
istream& operator >>(istream& in,Cartesian& c){
char next[MAX];
cout<<"Enter the 'x' co-ordinate: " ;
in>>next;
c.x=c.input(next);
cout<<"Enter the 'y' co-ordinate: " ;
in>>next;
c.y=c.input(next);
return in;
}
ostream& operator <<(ostream& out,Cartesian& c){
out<<"(" <<c.x<<"," <<c.y<<")" <<" = " <<"(x,y)" ;
return out;
}
Cartesian Cartesian::operator +(Cartesian& c){
Cartesian temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
Cartesian Cartesian::operator -(Cartesian& c){
Cartesian temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
bool Cartesian::operator ==(Cartesian& c){
if (c.x==x && c.y==y)
return true ;
else
return false ;
}
Cartesian::operator Polar(){//problem here, compiler gives errors, don't know
//how the function definition should look
//code here
}
The problem is that I have the prototype in the interface file called "operator Polar()" but I don't know how the syntax looks int he implementation file. I have tried all the difference combinations (I think) but met with little success.
Could someone please show me the proper syntax for the application file so that the conversion function "operaor Polar()" will be recognized? This way, I don't have to put the code into the interface.
Thanks,
Mike
Jul 8, 2012 at 10:23pm UTC
1 2 3 4
Cartesian::operator Polar()
{
return (Polar(/*...*/ ));
}
As simple as that.
Wazzak
Last edited on Jul 8, 2012 at 10:23pm UTC
Jul 8, 2012 at 10:25pm UTC
That's what I did, but Visual Studio is underling it in red.
Mike
Jul 8, 2012 at 10:26pm UTC
What does the error say?
Wazzak
Jul 8, 2012 at 10:33pm UTC
"Error:class"Cartesian" has no member "operator Polar"
Jul 8, 2012 at 10:42pm UTC
The
Cartesian class needs to know about
Polar . Since you'll be invoking a constructor of
Polar during the return of
Cartesian::operator Polar() , you'll have to fully define
Polar before
Cartesian . For instance:
Polar.h
1 2 3 4 5 6 7 8 9
#if !defined POLAR_IMPL
#define POLAR_IMPL
struct Polar
{
// ...
};
#endif
Cartesian.h
1 2 3 4 5 6 7 8 9 10 11
#if !defined CARTESIAN_IMPL
#define CARTESIAN_IMPL
#include "Polar.h"
struct Cartesian
{
operator Polar(); // OK; "Cartesian" now knows about "Polar".
};
endif
On a side-note, I strongly recommend that you remove
using namespace std from any headers, because it can cause name conflicts.
Wazzak
Jul 8, 2012 at 11:13pm UTC
I will take your advice!
Thanks a lot,
Mike
Topic archived. No new replies allowed.