473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template and include files

Hi all,

I was porting some code over to GCC, when I came across some code. GCC gave
a compile error. I looked it over and to me at least it looked like it
should have never compiled. But this code was supposedly compiled on WATCOM
and green hills.

I am assuming GCC is correct in denying the code, but does anyone know how
another compiler would even accept it. Or has anyone come across this
'pattern' before.

Here's a rough outline of the code. Please ignore the syntax. This is just
a rough outline, and its not my code.

BEGIN CODE
***************************************
**************************
pool.h
**************************
template<class T>
class pool
{
....
T *getObject();
void foo(T *x);
};

template<class T>
class T *pool<T>::getObject()
{
....some code
}

template<class T>
void pool<T>::foo(T *x)
{
x->implied(); //implied assumed to be part of class T
}
**************************
control.h
**************************
class A; //forward declare

class B
{
...
pool<A> mypool;
}

class A
{
...
void implied();
B myB;
}

********************************

This to me is a circular reference. To construct B, the size of A needs to
be known (pool<A>). To contruct A, the size of B needs to be known. Even
if A is put in front of B, the same is still true. To be honest, I can't
remeber which came first from the code :).

I fixed it already by dynamically assigning the B within A...

Any ideas. Is there something special about templates that allows this to
occur?

Yamin
Jul 19 '05 #1
3 1753
Yamin wrote in news:SI*******************@news20.bellglobal.com:
Hi all,

I was porting some code over to GCC, when I came across some code.
GCC gave a compile error. I looked it over and to me at least it
looked like it should have never compiled. But this code was
supposedly compiled on WATCOM and green hills.

I am assuming GCC is correct in denying the code, but does anyone know
how another compiler would even accept it. Or has anyone come across
this 'pattern' before.

To answer that we would need to see code that exibited the behaviour,
I haven't checked but with the gaps filled in I would expect gcc (I
have 3.2/Mingw) to compile your example below.
Here's a rough outline of the code. Please ignore the syntax. This
is just a rough outline, and its not my code.

BEGIN CODE
***************************************
**************************
pool.h
**************************
template<class T>
class pool
{
...
T *getObject();
void foo(T *x);
};

template<class T>
class T *pool<T>::getObject()
{
...some code
}

template<class T>
void pool<T>::foo(T *x)
{
x->implied(); //implied assumed to be part of class T
}
**************************
control.h
**************************
class A; //forward declare

class B
{
...
pool<A> mypool;
}

class A
{
...
void implied();
B myB;
}

********************************

This to me is a circular reference. To construct B, the size of A
needs to be known (pool<A>).
Nope the size of pool<A> needs to be known. In this example that
isn't dependant on A.
To contruct A, the size of B needs to
be known. Even if A is put in front of B, the same is still true. To
be honest, I can't remeber which came first from the code :).

I fixed it already by dynamically assigning the B within A...

Any ideas. Is there something special about templates that allows
this to occur?


Not really though the real problem you encouterd may have.

Change the above to:

class A;
class pool
{
A *getObject();
void foo(A *x);
};

class B
{
pool mypool;
};

class A
{
void implied();
B myB;
};

A *pool::getObject()
{
//...some code
}

void pool::foo(A *x)
{
x->implied(); //implied assumed to be part of class T
}

I hope you can see from the above that the class pool isn't dependant
on having a complete defenition of A. Only is member function's are.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200...
Yamin wrote in news:SI*******************@news20.bellglobal.com:
Hi all,

I was porting some code over to GCC, when I came across some code.
GCC gave a compile error. I looked it over and to me at least it
looked like it should have never compiled. But this code was
supposedly compiled on WATCOM and green hills.

I am assuming GCC is correct in denying the code, but does anyone know
how another compiler would even accept it. Or has anyone come across
this 'pattern' before.


To answer that we would need to see code that exibited the behaviour,
I haven't checked but with the gaps filled in I would expect gcc (I
have 3.2/Mingw) to compile your example below.
Here's a rough outline of the code. Please ignore the syntax. This
is just a rough outline, and its not my code.

BEGIN CODE
***************************************
**************************
pool.h
**************************
template<class T>
class pool
{
...
T *getObject();
void foo(T *x);
};

template<class T>
class T *pool<T>::getObject()
{
...some code
}

template<class T>
void pool<T>::foo(T *x)
{
x->implied(); //implied assumed to be part of class T
}
**************************
control.h
**************************
class A; //forward declare

class B
{
...
pool<A> mypool;
}

class A
{
...
void implied();
B myB;
}

********************************

This to me is a circular reference. To construct B, the size of A
needs to be known (pool<A>).


Nope the size of pool<A> needs to be known. In this example that
isn't dependant on A.
To contruct A, the size of B needs to
be known. Even if A is put in front of B, the same is still true. To
be honest, I can't remeber which came first from the code :).

I fixed it already by dynamically assigning the B within A...

Any ideas. Is there something special about templates that allows
this to occur?


Not really though the real problem you encouterd may have.

Change the above to:

class A;
class pool
{
A *getObject();
void foo(A *x);
};

class B
{
pool mypool;
};

class A
{
void implied();
B myB;
};

A *pool::getObject()
{
//...some code
}

void pool::foo(A *x)
{
x->implied(); //implied assumed to be part of class T
}

I hope you can see from the above that the class pool isn't dependant
on having a complete defenition of A. Only is member function's are.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Ah, yes I see. Perhaps I should've looked for that situation of a sizeless
class. I've just become so used to seeing that pattern, that I
automatically assumed something was wrong with the code. Just to make sure
I'm not insane though:

If the class A had any member variables then there would be a
problem...correct? I'll make sure of that tomorrow at work.

Yamin
Jul 19 '05 #3
Yamin wrote in news:2E*******************@news20.bellglobal.com:

Ah, yes I see. Perhaps I should've looked for that situation of a
sizeless class. I've just become so used to seeing that pattern, that
I automatically assumed something was wrong with the code. Just to
make sure I'm not insane though:

If the class A had any member variables then there would be a
problem...correct? I'll make sure of that tomorrow at work.


Not really 'A' does have a member variable, a 'B', and it could have
more.

The reason it works is that 'class pool' only needed to now that 'A' is
a type-name, the "class A;" forward declaration in my example and
the "... <typename T> ..." (where T = A when pool<A> was instantiated)
in your example were sufficient to do this.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
5
by: Nomak | last post by:
Hello all, i have two template classes which needs each other. I tried to write some fwd decl / decl / impl in a good way but i can't get it to compile. Explanations: - .hh files are for...
0
by: Leslaw Bieniasz | last post by:
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...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
5
by: Jacky Yuk | last post by:
Hi all, I am new to c++ but using c for long time. Recently, I created a MFC GUI project by VC/C++ 6.0. Everything was fine until I wanted to use "template": template <typename T> class...
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
14
by: Jess | last post by:
Hello, I was told that if I have a template class or template function, then the definitions must be put into the header file where I put the declarations. On the other hand, it is generally...
3
by: djsuson | last post by:
I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.