By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

cross-calling class template methods

P: n/a

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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*

Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.