Hello,
I'm creating a money-management program, and have run into a few related problems regarding std::vector s of my objects. Let's just dump it out:
Windows Vista (on my new laptop), Dev-C++
error-spawning lines are in red.
Code:C:/Dev-Cpp/include/c++/3.4.2/bits/vector.tcc: In member function `transaction& transaction::operator=(const transaction&)': C:/Dev-Cpp/include/c++/3.4.2/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = transaction, _Alloc = std::allocator<transaction>]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = transaction, _Alloc = std::allocator<transaction>]' account.cpp:17: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/vector.tcc:238: error: non-static const member `const Date transaction::date_created', can't use default assignment operator C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h: In function `const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = transaction]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:2484: instantiated from `void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<transaction*, std::vector<transaction, std::allocator<transaction> > >, _Size = int]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:2555: instantiated from `void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<transaction*, std::vector<transaction, std::allocator<transaction> > >]' account.cpp:18: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:90: error: passing `const transaction' as `this' argument of `bool transaction::operator<(const transaction&)' discards qualifiers C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:91: error: passing `const transaction' as `this' argument of `bool transaction::operator<(const transaction&)' discards qualifiers C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:93: error: passing `const transaction' as `this' argument of `bool transaction::operator<(const transaction&)' discards qualifiers C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:97: error: passing `const transaction' as `this' argument of `bool transaction::operator<(const transaction&)' discards qualifiers C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:99: error: passing `const transaction' as `this' argument of `bool transaction::operator<(const transaction&)' discards qualifiersI think that is has to do with my operator <Code:double account::transact(Date date_in, const std::string & name_in, double amount_in, bool nosort) { bal += amount_in; registry.push_back(transaction(date_in,name_in,amount_in,bal)); if(!nosort) std::sort(registry.begin(),registry.end()); return bal; }
And, finally... the involed objects.....Code:bool transaction::operator< (const transaction & other) { return date < other.date; } //..... elsewhere...... inline bool Date::operator< (const Date & x) { return y == x.year() ? ( m == x.month() ? d < x.day() : m < x.month() ) : y < x.year(); //years are not same -> compare years //years are the same -> (months are not same -> compare months) // (months are the same -> compare days) }
Code:class Date { public: enum Spec { today, yesterday, tomorrow }; enum Month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; Date(Month month_in = Month(0), int day_in = 0, int year_in = 0); Date(Spec s); std::string str(); int day() const { return d; } int month() const { return m; } int year() const { return y; } bool operator < (const Date & x); private: int d; int y; Month m; }; extern Date::Month & operator--(Date::Month &m); extern Date::Month & operator++(Date::Month &m);Code:class transaction { public: transaction( const Date& date_in = Date(), const std::string& name_in = std::string(), double amount_in = 0.0, double new_bal_in = 0.0, const Date& date_created_in = Date(Date::today) ); ~transaction() {} std::string file_rep(); std::string display_rep(); bool operator< (const transaction & other) { return date < other.date; } Date date; const Date date_created; std::string name; double amount; double new_bal; }; extern std::ostream & operator << (std::ofstream & s, transaction & tran); extern std::ostream & operator << (std::ostream & s, transaction & tran);Any help would be greatly appreciated.Code:class account { public: account(const std::string & name_in); ~account(); double transact(Date date_in, const std::string & name_in, double amount_in, bool nosort = false); std::string display(unsigned int num = 0); //shows the last num transactions. //0 means show all. const std::string & name() const { return n; } double balance() const { return bal; } private: std::vector<transaction> registry; double bal; std::string n; };



LinkBack URL
About LinkBacks
), Dev-C++



CornedBee