473,387 Members | 1,592 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,387 software developers and data experts.

How should a number-class look like?

What is wrong with the following? Why doesn't it compile?

template<int f, class me, typename ValType=int>
class Fm
{
protected:
ValType v;
public:
me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
};

class F0m : public Fm<0,F0m>
{
friend class Fm<0,F0m>;
int operator*(const F0m& o) const {return v*o.v;}
int operator/(const F0m& o) const {return v/o.v;}
int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}
public:
F0m() {}
F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
};

int main(...){
F0m n,p,z;
n=-1;
p=4;
z=234;
p=n*n;
n*=p;
z*=n;
z=n+p;
z=p-p;
n=p+p;
n+=z;
p-=z;
p=n/n;
n/=p;
/* assert(n!=p && p!=z && z!=n);*/
}
--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...

P
Jan 4 '07 #1
2 1292
Piotr Sawuk wrote:
What is wrong with the following? Why doesn't it compile?
What errors are you getting?
>
template<int f, class me, typename ValType=int>
class Fm
{
protected:
ValType v;
public:
me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
};

class F0m : public Fm<0,F0m>
{
friend class Fm<0,F0m>;
int operator*(const F0m& o) const {return v*o.v;}
int operator/(const F0m& o) const {return v/o.v;}
int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}
public:
F0m() {}
F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
};

int main(...){
F0m n,p,z;
n=-1;
p=4;
z=234;
p=n*n;
n*=p;
z*=n;
z=n+p;
z=p-p;
n=p+p;
n+=z;
p-=z;
p=n/n;
n/=p;
/* assert(n!=p && p!=z && z!=n);*/
}
Jan 4 '07 #2
Piotr Sawuk wrote:
What is wrong with the following? Why doesn't it compile?

template<int f, class me, typename ValType=int>
class Fm
{
protected:
ValType v;
public:
me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
};

class F0m : public Fm<0,F0m>
{
friend class Fm<0,F0m>;
int operator*(const F0m& o) const {return v*o.v;}
int operator/(const F0m& o) const {return v/o.v;}
int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}
Remember that name look up happens before access check. Simply by making
the above overloaded operators private doesn't mean the compile won't
have a hard time to compare those with other candidates.
public:
F0m() {}
F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
};

int main(...){
F0m n,p,z;
n=-1;
p=4;
z=234;
p=n*n;
n*=p;
z*=n;
z=n+p;
z=p-p;
n=p+p;
n+=z;
p-=z;
p=n/n;
n/=p;
/* assert(n!=p && p!=z && z!=n);*/
}

Regards,
Ben
Jan 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
1
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the...
13
by: Kishor | last post by:
Hi Friends Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32 Output : 5th root of 32 is 2 Ex:(1) Input : 243 Output : 5th root of 243 is...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
by: silversnake | last post by:
I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not. can anyone see what the problem is ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.