Title: | C++ |
Notice: | Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS) |
Moderator: | DECCXX::AMARTIN |
Created: | Fri Nov 06 1987 |
Last Modified: | Thu Jun 05 1997 |
Last Successful Update: | Fri Jun 06 1997 |
Number of topics: | 3604 |
Total number of notes: | 18242 |
Some more compilation problems with the STL I've had with porting from NT is with the following code. Again I can't see what I'm doing wrong, so has anyone got any ideas. #include <list> class X { public: X(); void func(const X& otherX); private: list<int> m_values; }; X::X() { m_values.push_back(1); } void X::func(const X& otherX) { list<int>::iterator current; current = otherX.m_values.begin(); } main(void) { X first; X second; first.func(second); } The error I get from DEC C++ V5.4-010 on OpenVMS VAX V6.2 is: current = otherX.m_values.begin(); ..................^ %CXX-E-NOCONVERTCLS, In this statement, "otherX.m_values.begin()" has a class type and cannot be converted to a class type that is incompatible. At line number 21 in USER1:[HARDIE.SOURCES]TMP2.CXX;1. } %VCG-I-NOBJECT, No object file produced. At line number 30 in USER1:[HARDIE.SOURCES]TMP2.CXX;1. %VCG-I-SUMMARY, Completed with 1 error(s), 0 warning(s), and 1 informational messages. At line number 30 in USER1:[HARDIE.SOURCES]TMP2.CXX;1. Thanks, Neil ----
T.R | Title | User | Personal Name | Date | Lines |
---|---|---|---|---|---|
3537.1 | The iterator has to be const | DECC::J_WARD | Fri Apr 11 1997 13:23 | 30 | |
In X::func() the type of otherX is const. Therefore otherX.m_values.begin() will return a const_iterator not an iterator. A const_iterator will not allow you to change the values inside otherX. Change: list<int>::iterator current; to list<int>::const_iterator current; and it will compile (BTW, because of automatic instantiation restrictions the declaration and definitions of X should be in its own header file...) BTW, our 6.0 compiler will give you a better error message: cosf.zko.dec.com> $CEXE/deccxx_driver -I$HOME/RWEDG2/dec_stdlib/cms_headers t.cx x cxx: Error: X.h, line 19: no suitable user-defined conversion from "list<in t, allocator<int>>::const_iterator" to "const list<int, allocator <int>>::iterator" exists current = otherX.m_values.begin(); ------------------------------------------^ 1 error detected in the compilation of "t.cxx". |