Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I am inheriting CTimeSpan class in my base class CVoipTimeSpan. I am printing all the member functions like GetDays(), GetMinuts().. it gives negative values, how can i get positive values. I am using CVoipTimeSpan in place of CTimeSpan in my project. In this it shows some errors like
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CTimeSpan' (or there is no acceptable conversion)
can we inherit the operators or not?
please give me the reply.

[EDIT]

Hello After inheriting CTimeSpan class to my class CVoipTimeSpan the operater '=' is not inherited then i write the code to inheriting '=' operator as

C++
const CVoipTimeSpan& CVoipTimeSpan::operator=(const CVoipTimeSpan& entry)
{
    if (entry != *this)
    {
        (CTimeSpan)*this = (CTimeSpan) entry;
    }
    return *this;
}


I tested this code in following statements

C++
CTime curr_time;
CVoipTimeSpan time_span;
int time_diff_in_secs;
curr_time = CTime::GetCurrentTime();
time_span = curr_time - database_cache_lookup_time;
time_diff_in_secs = time_span.GetTotalSeconds();
Posted
Updated 5-May-11 5:46am
v4
Comments
Sergey Alexandrovich Kryukov 7-Apr-11 2:24am    
Gosh, this is your class now, how do we know how why do you get negative values. Perhaps you subtract later time from the earlier time? :-)
--SA

The problem with operator is simple: you inherit it of course, but it defines the operators on the base class, but you try to operate with your derived class, and you probably did not define implicit conversion operator. So, you need to define the operator(s) you need to include your derived type: assignment and whatever else you may need. You also may need to define conversions and copy constructors.

As to your negative values, see my comment to the Question. If you cannot figure it out, add some code, but please keep it to essential minimum.

[EDIT]
(After additional information.)

You're trying to define assignment operator for assigning CVoipTimeSpan to CVoipTimeSpan, but your error is about assigning CTimeSpan to CVoipTimeSpan. You're not trying to define this operator. Use the argument (const CTimeSpan& entry); it will represent right-hand part of assignment.

As to a negative time span I don't see a problem here: It time T1 is sooner than time T2, T2-T1 should be positive time span, and T1-T2 should be negative time span, isn't that natural?

—SA
 
Share this answer
 
v3
Comments
vallikelam 6-May-11 1:13am    
I solved the negative values and defined the all constructors in my class that is not the problem only operator = doesn't inherited u said use argument (const CTimeSpan& entry) i also tried it but that doesn't taken it shows some ambiguous occurs i think some assignments are there i tried my best if u know please tell me
Sergey Alexandrovich Kryukov 6-May-11 1:15am    
If you want to address some problem, please report it very accurately.
--SA
vallikelam 6-May-11 2:01am    
Thank u sir for advising me i got the solution no need to define operator = in derived class it inherits = operator, why it didn't work long back when i tested i don't know, now i got the time_diff_in_secs.
Sergey Alexandrovich Kryukov 6-May-11 2:34am    
You're welcome.
So, no more problems?
Will you consider formally accepting this answer (green button)?
Thank you.
--SA
Niklas L 6-May-11 4:29am    
Fived.
Maybe this has changed since VS 2003 which I'm using at the moment, but I see no
CTimeSpan::operator=()
defined here, so you couldn't possibly inherit from it. The compiler will automatically create an operator= for any class that doesn't define one itself, but it will not be virtual, and thus can not be inherited and overridden, only overwritten!

Moreover, even if there were a method
virtual CTimeSpan::operator=(const CTimeSpan&)
then to override it you must use the same types for the parameter, in this case const CTimeSpan&, not const CVoipTimeSpan&. This would be bad design however, since it would allow assignments of instances of class CTimeSpan or other classes derived from it, which are potentially not compatible to CVoipTimeSpan. Even if there would be no compatibility issues now, when someone later decides to expand your class CVoipTimeSpan, this might create such an incompatibility and thus invalidate not only your implementation of operator=(), but also, possibly, many of the places where it has been invoked!

I've already pointed out in another thread of yours how to properly implement
CVoipTimeSpan::operator=(const CVoipTimeSpan&)
, and as far as I can see all other questions have been answered.

On a sidenote, I've seen your question posted at least three times now! Do not do that! It will just keep people who might be able to help you ignorant of additional information and advice that's already been handed out. Basically you multiply the workload on other people while getting confusing responses to different threads that are based on different assumptions. Instead, just edit your original question!

It's a bit late now. However, do us the favor to at the very least:
- mark all of your threads as answered if your problem is solved, or
- mark all but one of your threads as answered if there are still questions
- edit the original question in each of your threads to make others aware of the duplicate threads, and maybe insert a link to that one thread that you would like any additional answers in.
 
Share this answer
 
v2
Comments
Niklas L 6-May-11 4:31am    
Very good comment. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900