|
-
Oct 17th, 2002, 02:34 PM
#1
Thread Starter
Addicted Member
Type Conversion Errors......
Can anyone help me with an easy way to fix casting problems in C++. I am using VisualC++ and when I try to make anything I always get so many gay errors. For ex:
cannot convert 'struct MyStruct' to NULL or there is no acceptable conversion...
and so on... In Java, most anything can be set to null, is there a way to do the same in C++? What is the point of casting ( (UINT)MyDWORD ) if it doesn't work?? Someone plz help!
Also, can someone explain how to overload an operator for a class?
To protect time is to protect everything...
-
Oct 17th, 2002, 04:46 PM
#2
In Java you can't set everything to NULL. You can only set references to NULL. It's just that in Java you only deal with references.
You can set every pointer in C++ to NULL, so you break even here. You can only set things to 0 memberwise. Like
struct st {
int a, b;
};
st s;
s.a = s.b = 0;
I have something on operator overloading, hold on.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 04:47 PM
#3
Seems to be lost. I'll write something. Stand by.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 05:44 PM
#4
Code:
// COp
// A class that overloads all operators possible
// Has two data members: i1 and i2
// does stupid things with them
class COp
{
// data
int i1, i2;
public:
// standard constructor
COp() { i1=i2=0; };
// copy constructor
COp(const COp &o) { i1=o.i1; i2=o.i2; };
// destructor
~COp() {};
// section 1: simple assignment operator
const COp &operator =(const COp &o);
const COp &operator =(int i);
const COp &operator =(double d);
const COp &operator =(const std::string &s);
// section 2: normal arithmetic operators
COp operator +(const COp &o);
COp operator -(const COp &o);
COp operator *(const COp &o);
COp operator /(const COp &o);
COp operator %(const COp &o);
COp operator -();
COp operator +();
// section 2a: normal arithmetic assignment operators
const COp &operator +=(const COp &o);
const COp &operator -=(const COp &o);
const COp &operator *=(const COp &o);
const COp &operator /=(const COp &o);
const COp &operator %=(const COp &o);
// section 3: bit operators
COp operator <<(int i);
COp operator >>(int i);
COp operator &(const COp &o);
COp operator ^(const COp &o);
COp operator |(const COp &o);
COp operator ~();
// section 3a: bit assignment operators
const COp &operator <<=(int i);
const COp &operator >>=(int i);
const COp &operator &=(const COp &o);
const COp &operator ^=(const COp &o);
const COp &operator |=(const COp &o);
// section 4: comparison operators
bool operator ==(const COp &o);
bool operator !=(const COp &o);
bool operator <=(const COp &o);
bool operator >=(const COp &o);
bool operator >(const COp &o);
bool operator <(const COp &o);
// section 5: conversion operators
// section 5a: conversion constructors
COp(int i);
COp(double d);
COp(bool b);
COp(const std::string &s);
// section 5b: cast operators
operator int();
operator double();
operator bool();
operator std::string ();
// section 6: increment/decrement
// prefix
const COp &operator ++();
const COp &operator --();
// postfix
COp operator ++(int);
COp operator --(int);
// section 7: misc.
// function operator - you can treat the object like a function
void operator ()();
int operator ()(int i);
// indexing operator - treat the object like an array
int &operator [](int i);
int &operator [](const std::string &s);
// dereferencing operator - treat the object like a pointer to something
// commented and not implemented because it doesn't make any sense in this context
// type operator *();
// same goes for the -> operator
// type *operator ->();
};
// implementation
const COp & COp::operator =(const COp &o)
{
i1 = o.i1;
i2 = o.i2;
return *this;
}
const COp & COp::operator =(int i)
{
i1 = i2 = i;
return *this;
}
const COp & COp::operator =(double d)
{
i1 = i2 = d;
return *this;
}
const COp & COp::operator =(const std::string &s)
{
std::istringstream is(s);
s >> i1 >> i2;
return *this;
}
COp COp::operator +(const COp &o)
{
COp t(*this);
t += o;
return t;
}
COp COp::operator -(const COp &o)
{
COp t(*this);
t -= o;
return t;
}
COp COp::operator *(const COp &o)
{
COp t(*this);
t *= o;
return t;
}
COp COp::operator /(const COp &o)
{
COp t(*this);
t /= o;
return t;
}
COp COp::operator %(const COp &o)
{
COp t(*this);
t %= o;
return t;
}
COp COp::operator -()
{
COp t;
t.i1 = -i1;
t.i2 = -i2;
return t;
}
COp COp::operator +()
{
COp t;
t.i1 = abs(i1);
t.i2 = abs(i2);
return t;
}
const COp &COp::operator +=(const COp &o)
{
i1 += o.i1;
i2 += o.i2;
return *this;
}
const COp &COp::operator -=(const COp &o)
{
i1 -= o.i1;
i2 -= o.i2;
return *this;
}
const COp &COp::operator *=(const COp &o)
{
i1 *= o.i1;
i2 *= o.i2;
return *this;
}
const COp &COp::operator /=(const COp &o)
{
i1 /= o.i1;
i2 /= o.i2;
return *this;
}
const COp &COp::operator %=(const COp &o)
{
i1 %= o.i1;
i2 %= o.i2;
return *this;
}
COp COp::operator <<(int i)
{
COp t(*this);
t <<= i;
return t;
}
COp COp::operator >>(int i)
{
COp t(*this);
t >>= i;
return t;
}
COp COp::operator &(const COp &o)
{
COp t(*this);
t &= o;
return t;
}
COp COp::operator ^(const COp &o)
{
COp t(*this);
t ^= o;
return t;
}
COp COp::operator |(const COp &o)
{
COp t(*this);
t |= o;
return t;
}
COp COp::operator ~()
{
COp t;
t.i1 = ~i1;
t.i2 = ~i2;
return t;
}
const COp &COp::operator <<=(int i)
{
i1 <<= i;
i2 <<= i;
return *this;
}
const COp &COp::operator >>=(int i)
{
i1 >>= i;
i2 >>= i;
return *this;
}
const COp &COp::operator &=(const COp &o)
{
i1 &= o.i1;
i2 &= o.i2;
return *this;
}
const COp &COp::operator ^=(const COp &o)
{
i1 ^= o.i1;
i2 ^= o.i2;
return *this;
}
const COp &COp::operator |=(const COp &o)
{
i1 |= o.i1;
i2 |= o.i2;
return *this;
}
bool COp::operator ==(const COp &o)
{
return (i1 == o.i1 && i2 == o.i2);
}
bool COp::operator !=(const COp &o)
{
return (i1 != o.i1 || i2 != o.i2);
}
bool COp::operator <=(const COp &o)
{
return ((int)*this) <= ((int)o);
}
bool COp::operator >=(const COp &o)
{
return ((int)*this) >= ((int)o);
}
bool COp::operator >(const COp &o)
{
return ((int)*this) > ((int)o);
}
bool COp::operator <(const COp &o)
{
return ((int)*this) < ((int)o);
}
COp::COp(int i)
{
i1 = i2 = i;
}
COp::COp(double d)
{
i1 = i2 = d;
}
COp::COp(bool b)
{
i1 = 0;
i2 = b?0:1;
}
COp::COp(const std::string &s)
{
std::istringstream is(s);
is >> i1 >> i2;
}
COp::operator int()
{
return (i1+i2)/2;
}
COp::operator double()
{
return (i1+i2)/2.0;
}
COp::operator bool()
{
return i1 == i2;
}
COp::operator std::string ()
{
std::ostringstream os;
os << i1 << ' ' << i2;
return os.str();
}
const COp &COp::operator ++()
{
++i1;
++i2;
return *this;
}
const COp &COp::operator --()
{
--i1;
--i2;
return *this;
}
COp COp::operator ++(int)
{
COp t(*this);
++*this;
return t;
}
COp COp::operator --(int)
{
COp t(*this);
--*this;
return t;
}
void COp::operator ()()
{
// do something stupid here
}
int COp::operator ()(int i)
{
// another stupid thing
return i1*i+i2/i;
}
int &COp::operator [](int i)
{
if(i == 0)
return i1;
else if(i == 1)
return i2;
else
throw std::out_of_range("COp::operator [](int) called with invalid value");
}
int &COp::operator [](const std::string &s)
{
if(s == "first")
return i1;
else if(s == "second")
return i2;
else
throw std::out_of_range("COp::operator [](std::string) called with invalid value");
}
I leave it to you as an exercise to add global +,-,*,/,% operators for one COp and one basic type operand.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 05:45 PM
#5
requires these includes to work:
#include <string>
#include <sstream>
#include <cmath>
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|