I have a frustrating task of porting some highly templated code I wrote using MS visual studio to compile and run via gcc. In my complex header file I have several classes that reference each other. That's no problem in C++ in theory since you are supposed to be able to use 'forward declarations' within your header.
I took an example from this site,
http://www.devx.com/tips/Tip/12583
Well his example didn't compile, so I tweaked it a little until it compiled on MS Visual Studio. Here is the result:
Code:
//file: bank.h
class Account; //forward declaration
class Report{
public:
//fine: Account is known to be a not-yet defined class
void Output(const Account& account)
{} ;
};
class Account {
Report rep;
public:
void Show() {
rep.Output(*this);
}
};
int main(int argc, char *argv[])
{
Account A ;
Report R ;
R.Output(A) ;
}
So vis. studio 2005 compiles this but gcc 4.3.0 gives the error:
test2.cpp:13: error: cannot call member function \u2018void Report::Output(const Account&)\u2019 without object
In the actual code I'm trying to compile with gcc I have something like this:
Code:
// some forward declarations
class dynmatrix ;
template <int nrows, int ncols, int ld = nrows>
class cmatrix ;
template <int nrows, int ncols, int ld = nrows>
class rmatrix ;
template<int nrows, int ncols, int ld>
protected:
int issub ;
public:
float *val ;
/* construct from a dynamic matrix */
rmatrix(dynmatrix &dm)
{
// construction code
}
// lots more stuff
} ; // end rmatrix class
// actual dynmatrix code
class dynmatrix {
protected:
int issub ;
public:
float *val ;
// ...
// some code that references rmatrix
template<int nr, int nc, int uld>
dynmatrix(rmatrix<nr,nc,uld> &cm) : nrows(nr), ncols(nc), ld(uld)
{
mtype = Realmatrix ;
val = cm.tohost() ;
}
} ; // end class dymmatrix
This bit of code is giving me errors on the forward declarations, namely
matrix.h: In constructor \u2018rmatrix<nrows, ncols, ld>::rmatrix(dynmatrix&)\u2019:
matrix.h:406: error: invalid use of incomplete type \u2018struct dynmatrix\u2019
matrix.h:80: error: forward declaration of \u2018struct dynmatrix\u2019
The first error is in the rmatrix constructor that takes a dynmatrix argument and the second is in the forward declaration itself.
Now if you can't do forward declarations in gcc 4.3, that's going to break a lot of code. Is there some kind of work around for this?