Vector of Classes w/ overloaded operators notcompiling
I am working on a school project and am stuck in the debugging. I am compiling with g++ and am using c++11.
Error message
1 2 3 4 5 6 7 8 9 10 11 12 13
|
In file included from /usr/include/c++/4.7/algorithm:63:0,
from date.h:7,
from date.cpp:1,
from schedule.h:1,
from schedule.cpp:1,
from date_driver.cpp:1:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Date*, std::vector<Date> >; _Tp = Date]’:
/usr/include/c++/4.7/bits/stl_algo.h:2309:70: required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Date*, std::vector<Date> >]’
/usr/include/c++/4.7/bits/stl_algo.h:2340:54: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Date*, std::vector<Date> >; _Size = int]’
/usr/include/c++/4.7/bits/stl_algo.h:5476:4: required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Date*, std::vector<Date> >]’
schedule.cpp:92:50: required from here
/usr/include/c++/4.7/bits/stl_algo.h:2271:4: error: passing ‘const Date’ as ‘this’ argument of ‘bool Date::operator<(Date)’ discards qualifiers [-fpermissive]
|
I have a vector of a date class that I am trying to sort within another class and this is my sort function that is giving me an error.
1 2 3 4 5
|
void schedule :: sortDates (){
sort (mustRemember.begin (), mustRemember.end ());
}
|
Here are my overloaded operators for my date class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
bool Date :: operator == (Date otherDate){
if (((otherDate.getDay () == getDay ()) && (otherDate.getMonth () == getMonth ())) && (otherDate.getYear () == getYear ()))
return true;
else
return false;
}
bool Date :: operator < (Date otherDate){
if (getYear () < otherDate.getYear ())
return true;
else if (getYear () > otherDate.getYear ())
return false;
else if (getMonth () < otherDate.getMonth ())
return true;
else if (getMonth () > otherDate.getMonth ())
return false;
else if (getDay () < otherDate.getDay ())
return true;
else
return false;
}
|
Let me know if I should include any more information.
Last edited on
your problem is const-correctness.
mark any member function that does not modify the sate of the object as const
Topic archived. No new replies allowed.