RE: [Flightgear-devel] const and copy constructor
> Alistair K Phipps writes: > > > > or use vector< FGTable* > rather than vector< FGTable >, > > Yes I prefer using this form too > > Norman I made a few small changes to use the pointer as described above by Alistair. That took care of the problem. Thanks guys. Jon ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
RE: [Flightgear-devel] const and copy constructor
Alistair K Phipps writes: > > or use vector< FGTable* > rather than vector< FGTable >, Yes I prefer using this form too Cheers Norman ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
RE: [Flightgear-devel] const and copy constructor
Jon Berndt writes: > > > Can you give a complete minimal example? I just tried the > > following one, and this compiles without problems, as it should: > > See below. > > > Are you *using* the assignment operator for your class > > somewhere? > > Not that I know of. see below > FGTable::FGTable(const FGTable& t) : >PropertyManager(t.PropertyManager) > { > Type = t.Type; > colCounter = t.colCounter; > rowCounter = t.rowCounter; > tableCounter = t.tableCounter; > nRows = t.nRows; > nCols = t.nCols; > nTables = t.nTables; > > Tables = t.Tables; ^^ try something like < untested > Tables.resize(nTables); for( i=0; ihttp://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
Jon Berndt wrote: Are you *using* the assignment operator for your class somewhere? Not that I know of. FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager) { Tables = t.Tables; I think the problem is this assignment of vector< FGTable > which involves assignment of individual FGTable instances. The default operator= does not cope with copying non-static const members. You'll either have to define: const FGTable& FGTable::operator=( const FGTable& t ) { // defn goes here } or use vector< FGTable* > rather than vector< FGTable >, depending on what you're wanting to do. Minimal code that demonstrates the problem: #include class A { public: A( void* const p ) : _p( p ) {} A(const A &a) : _p( a._p ) { va = a.va; } private: std::vector< A > va; void * const _p; }; int main( void ) { A *pa = new A( 0 ); A pa2 = *pa; return 0; } HTH, Alistair ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
RE: [Flightgear-devel] const and copy constructor
> But I don't know at the moment, why Jons code doesn't work :( > > CU, > Christian For the moment, I'll just make it non-const. Jon ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
RE: [Flightgear-devel] const and copy constructor
> Can you give a complete minimal example? I just tried the > following one, and this compiles without problems, as it should: See below. > Are you *using* the assignment operator for your class > somewhere? Not that I know of. > Cheers > -Gerhard Header file for FGTable: --- start --- #include "FGConfigFile.h" #include "FGXMLElement.h" #include "FGParameter.h" #include "FGPropertyManager.h" #include #include using std::vector; using std::stringstream; namespace JSBSim { class FGTable : public FGParameter { public: ~FGTable(); FGTable(const FGTable& table); FGTable (FGPropertyManager* propMan, Element* el); double GetValue(void) const {}; double GetValue(double key); double GetValue(double rowKey, double colKey); double GetValue(double rowKey, double colKey, double TableKey); void operator<<(FGConfigFile&); void operator<<(stringstream&); FGTable& operator<<(const double n); FGTable& operator<<(const int n); inline double GetElement(int r, int c) {return Data[r][c];} inline double GetElement(int r, int c, int t); void Print(void); private: enum type {tt1D, tt2D, tt3D} Type; double** Data; vector Tables; int nRows, nCols, nTables; int colCounter, rowCounter, tableCounter; int lastRowIndex, lastColumnIndex, lastTableIndex; double** Allocate(void); FGPropertyManager* const PropertyManager; void Debug(int from); }; } --- end --- Source file snippet: --- start --- FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager) { Type = t.Type; colCounter = t.colCounter; rowCounter = t.rowCounter; tableCounter = t.tableCounter; nRows = t.nRows; nCols = t.nCols; nTables = t.nTables; Tables = t.Tables; Data = Allocate(); for (int r=0; r<=nRows; r++) { for (int c=0; c<=nCols; c++) { Data[r][c] = t.Data[r][c]; } } lastRowIndex = t.lastRowIndex; lastColumnIndex = t.lastColumnIndex; lastTableIndex = t.lastTableIndex; } --- end --- The exact error message given from the Borland compiler is: "_algobase.h": E2125 Compiler could not generate operator= for class 'JSBSim::FGTable' in function _STL::JSBSim::FGTable * __copy(const JSBSim::FGTable *,const JSBSim::FGTable *,JSBSim::FGTable *,const random_access_iterator_tag &,int *) at line 145 The exact error message given from the gcc compiler in CygWin is: g++ -I. -Isimgear/props -oFGTable.o -c FGTable.cpp /usr/include/c++/3.3.1/bits/stl_algobase.h: In member function ` JSBSim::FGTable& JSBSim::FGTable::operator=(const JSBSim::FGTable&)': /usr/include/c++/3.3.1/bits/stl_algobase.h:241: instantiated from `_OutputIter std::__copy(_RandomAccessIter, _RandomAccessIter, _OutputIter, std::random_access_iterator_tag) [with _RandomAccessIter = JSBSim::FGTable*, _OutputIter = JSBSim::FGTable*]' /usr/include/c++/3.3.1/bits/stl_algobase.h:241: error: non-static const member `JSBSim::FGPropertyManager* const JSBSim::FGTable::PropertyManager', can't use default assignment operator ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
RE: [Flightgear-devel] const and copy constructor
> I think it complains the class has no constructor other than the > copy one. > > you should try to add one : > > class MyClass { > public: > MyClass(AnotherClass *p); // constructor > MyClass(const MyClass &mc); // copy constructor > > private: > AnotherClass* const ptrToAnotherClass; > > } > > MyClass::MyClass(AnotherClass *p) : > ptrToAnotherClass(p) > { > // other assignments > } I actually do have one, I just omitted it for clarity. Jon ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
Mathias Fröhlich schrieb: On Freitag 10 Dezember 2004 03:40, Jon Berndt wrote: This gives me an odd error: non-static const member `AnotherClass* const ptrToAnotherClass', can't use default assignment operator If I remove the const specifier from the declaration for ptrToAnotherClass, and then move the assignment at the copy constructor into the code (instead of doing the "const thing" in the initializer list), then the compile works. That is something which is, I believe, missinterpreted by some compilers. You have a constant value and as such it cannot change. Initial assignment is a change and so you cannot assign that one. Your compiler seems to allow such an assignment for static members, but not for nonstatic members. When the initial assignment happens in such a way as Jon has written it foo( const bar& b ) : foobar( b.foobar ) { // whatever } it is fine. When the assignment happens inside the contructor (or anywhere else), like foo( const bar& b ) { foobar = b.foobar; // whatever } then it can't work due to the const of foobar. But I don't know at the moment, why Jons code doesn't work :( CU, Christian ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
Mathias Fröhlich wrote: That is something which is, I believe, missinterpreted by some compilers. You have a constant value and as such it cannot change. Initial assignment is a change and so you cannot assign that one. Your compiler seems to allow such an assignment for static members, but not for nonstatic members. It can be done the following way: In the header file: class FOO { private: const char *ptr; } In the cpp file: const char *FOO::ptr = "dummy text"; Erik ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
Hmm... Can you give a complete minimal example? I just tried the following one, and this compiles without problems, as it should: =< snip >== struct bar {} ; struct foo { foo( foo const& rhs ) : p( rhs.p ) {} bar* const p ; } ; =< snip >== Are you *using* the assignment operator for your class somewhere? Cheers -Gerhard -- Gerhard Wesp o o Tel.: +41 (0) 43 5347636 Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/ CH-8045 Zuerich \_/ See homepage for email address! ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
On Freitag 10 Dezember 2004 03:40, Jon Berndt wrote: > This gives me an odd error: > > non-static const member `AnotherClass* const ptrToAnotherClass', can't use > default assignment operator > > If I remove the const specifier from the declaration for ptrToAnotherClass, > and then move the assignment at the copy constructor into the code (instead > of doing the "const thing" in the initializer list), then the compile > works. That is something which is, I believe, missinterpreted by some compilers. You have a constant value and as such it cannot change. Initial assignment is a change and so you cannot assign that one. Your compiler seems to allow such an assignment for static members, but not for nonstatic members. That is some kind of braindead, but it is present ... I am not shure what the C++ standard tells about that, I think I will read that today :) Greetings Mathias -- Mathias Fröhlich, email: [EMAIL PROTECTED] ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d
Re: [Flightgear-devel] const and copy constructor
Jon Berndt wrote: > I have a situation where I am getting an error and I am not sure why. I have > a class > MyClass that has a copy constructor. The class has a private member that is a > const > pointer (the pointer is constant - not what the pointer points to): > > class MyClass { > public: > MyClass(const MyClass &mc); // copy constructor > > private: > AnotherClass* const ptrToAnotherClass; > > } > > The copy constructor needs to copy the const element ptrToAnotherClass to be > complete. I > do it like this: > > MyClass::MyClass(const MyClass &mc) : > ptrToAnotherClass(mc.ptrToAnotherClass) > { > // other assignments > } > > This gives me an odd error: > > non-static const member `AnotherClass* const ptrToAnotherClass', can't use > default > assignment operator > > If I remove the const specifier from the declaration for ptrToAnotherClass, > and then move > the assignment at the copy constructor into the code (instead of doing the > "const thing" > in the initializer list), then the compile works. > > Any suggestions? I think it complains the class has no constructor other than the copy one. you should try to add one : class MyClass { public: MyClass(AnotherClass *p); // constructor MyClass(const MyClass &mc); // copy constructor private: AnotherClass* const ptrToAnotherClass; } MyClass::MyClass(AnotherClass *p) : ptrToAnotherClass(p) { // other assignments } -Fred ___ Flightgear-devel mailing list [EMAIL PROTECTED] http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d