cross-calling class template methods

L

Leslaw Bieniasz

Cracow, 16.09.2004

Hi,

I have a problem with compiling the following construction involving
cross-calls of class template methods, with additional inheritance.
I want to have three class templates:

------------------------------------------
in file "Model.h":

#include "Data.h"

template<class T> class Model
{
....
void Read(Data<T> *data);
virtual int Get_Info(void);
};

template<class T>
void Model<T>::Read(Data<T> *data)
{
....
T something = data->Get(); // Note the Data method call
}

template<class T>
int Model<T>::Get_Info(void)
{
....
}

template class Model<float>;
template class Model<double>;

---------------------------------------
in file "Model1.h":

#include "Model.h"

template<class T> class Model1 : public Model<T>
{
....
virtual int Get_Info();
};

template<class T>
int Model1<T>::Get_Info(void)
{
....
}

template class Model1<float>;
template class Model1<double>;

----------------------------------------
in File "Data.h":

#include "Model.h"

template<class T> class Data
{
Data(Model<T> *model);
....
T Get(void);
};

template<class T>
Data<T>::Data(Model<T> *model)
{
....
int info = model->Get_Info(); // Note the virtual Model method call
}

template<class T>
T Data<T>::Get(void)
{
....
}

template class Data<float>;
template class Data<double>;

------------------------------------------
In the calling unit, file "Caller.h" I want to be able to do something
like this:

#include "Model.h"
#include "Model1.h"
#include "Data.h"

template<class T> class Caller
{
....
void call(void);
};

template<class T>
Caller<T>::call(void)
{
....
Model<T> *model = new Model1<T>();
....
Data<T> *data = new Data<T>(model);
....
model->Read(data);
....
}

template class Caller<float>;
template class Caller<double>;
---------------------------------------------------

I cannot figure out where to #include the various files and/or
where to provide forward class template declarations in order to avoid
compiler errors caused by nested inclusions of the header files, as
indicated. I have tried various combinations, such as not including
the headers, but using only forward declarations, or including the
headers after class declarations but before the method bodies. Nothing
helps.

I would appreciate any advice, also how to possibly redesign
the above code in order to have the same functionality and logic.
(I hope I have not introduced any spurious errors into the above
description, I am just writing this from memory, not from the
real code. In the real code certainly there are no typos or similar
errors. Please pay attention to the problem structure, not to details
of the statements). I use BCB 4.0, if this has any meaning.

Sincerely,

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top