Help...Operator Overloading...Problem

T

titan0111

----inside of class called snowfall, i wrote----

int operator == (char[]);


----and right before main() i wrote----

int snowfall::eek:perator == (char str[])

// comparing two member data (dates)

{
return (strcmp(date, str) == 0) ? 1 : 0;
}


----inside of main i called it----

while (sf.getdate() != "00/00")
{...}

i was told that this won't work. can you tell me what's wrong?
and give me the correct way to write it?
 
R

Ron Natalie

----inside of class called snowfall, i wrote----

int operator == (char[]);
while (sf.getdate() != "00/00")
{...}

i was told that this won't work. can you tell me what's wrong?
and give me the correct way to write it?

== and != are two distinct operators. You defined one but
are using the other.
 
V

Victor Bazarov

----inside of class called snowfall, i wrote----

int operator == (char[]);

Make it

bool operator==(const char*) const;
----and right before main() i wrote----

int snowfall::eek:perator == (char str[])

bool snowfall::eek:perator == (char const *str) const
// comparing two member data (dates)

{
return (strcmp(date, str) == 0) ? 1 : 0;

return strcmp(date, str) == 0;
}


----inside of main i called it----

while (sf.getdate() != "00/00")
{...}

i was told that this won't work. can you tell me what's wrong?


Not with much assuredness lacking the definition of 'snowfall' type
and the declaration of 'sf' here. But I suspect the idea is to do

while (sf != "00/00")
and give me the correct way to write it?

See above. And next time read the FAQ.

V
 
V

Victor Bazarov

Victor said:
(e-mail address removed) wrote: [...]
----inside of main i called it----

while (sf.getdate() != "00/00")
{...}

i was told that this won't work. can you tell me what's wrong?



Not with much assuredness lacking the definition of 'snowfall' type
and the declaration of 'sf' here. But I suspect the idea is to do

while (sf != "00/00")


And, yes, I missed the fact that you're using != having defined ==.
Ron is right. Pay attention what operator you're using.

V
 
T

titan0111

---in class i changed to----

int operator != (char[]);

---right after class i changed difinition to----

int snowfall::eek:perator != (char str[])

// overloading operator = to compare two strings

{
return (strcmp(date, str) == 0) ? 0 : 1;
}

----in main() i wrote----
while (sf.getdate() != "00/00")

and it won't still work....
 
V

Victor Bazarov

---in class i changed to----

int operator != (char[]);

---right after class i changed difinition to----

int snowfall::eek:perator != (char str[])

// overloading operator = to compare two strings

{
return (strcmp(date, str) == 0) ? 0 : 1;
}

----in main() i wrote----
while (sf.getdate() != "00/00")

and it won't still work....


Read the FAQ 5.8 (http://www.parashift.com/c++-faq-lite/)

V
 
K

Karl Heinz Buchegger

---in class i changed to----

int operator != (char[]);

---right after class i changed difinition to----

int snowfall::eek:perator != (char str[])

// overloading operator = to compare two strings

{
return (strcmp(date, str) == 0) ? 0 : 1;
}

----in main() i wrote----
while (sf.getdate() != "00/00")

and it won't still work....


Then please tell us what 'does not work' means!
Do you get an error message from the compiler? If yes,
which one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top