template header issues

Nov 24, 2009 at 1:34pm
Hi,
I'm trying to use a library that has some template classes, and thus I have some classes of my own now that inherit these templates. This means I end up having to include the whole code of my template classes in headers, or else I end up with missing objects at linking time. When I include the whole class in my headers, the compiler then no longer gets around classes referencing classes that reference themselves because of the incomplete type issues. I tried making different header files, and this simply got me redefinition errors. Does anyone have some suggestions of how to get around this problem? Normally just making a pointer visible in header files and referencing the functions in the *cpp files is the only solution I found so far, but templates seem to exclude this from my options.
Thanks in Advance,
Sean
Nov 24, 2009 at 3:19pm
When I include the whole class in my headers, the compiler then no longer gets around classes referencing classes that reference themselves because of the incomplete type issues.


Forward declare.

Relevent article:
http://cplusplus.com/forum/articles/10627/

Specifically, see section 7: Function inlining (since this typically applies to template classes)
And section 8: Forward declaring templates
Nov 24, 2009 at 4:24pm
Thanks,
That article was very helpful.
It turns out I had to use a typedef in a separate header file:

1
2
3
4
5
6
7
8
9
10
//in CFASTA_fwd.h:

#ifndef _CFASTA_FWD_H
#define	_CFASTA_FWD_H
#include "CHeaderAndSequence.h"
//template <typename T> class Tem;
//typedef Tem<int> A;
template <class SelectableItemType > class CFASTA;
typedef CFASTA<CHeaderAndSequence*> CFASTA_TYPE;
#endif	/* _CFASTA_FWD_H */ 


And then refer to the new type
CFASTA_TYPE*
instead of
CFASTA<CHeaderAndSequence*>*

This is working so far anyway.

Thanks,

Sean
Last edited on Nov 24, 2009 at 4:25pm
Nov 24, 2009 at 10:35pm
Ok, it's not working again:

1
2
3
4
5
6
7
//in CDrawSequence_fwd.h
#ifndef _CDRAWSEQUENCE_FWD_H
#define	_CDRAWSEQUENCE_FWD_H
#include<map>
class CDrawSequence;
typedef multimap<CHeaderAndSequence*,CDrawSequence*>::iterator mulimapIterator_TYPE;
#endif	/* _CDRAWSEQUENCE_FWD_H */ 


gives me the errors:


g++-4.0    -c -g -w -MMD -MP -MF build/Debug/GNU-MacOSX/CDrawSequence.o.d -o build/Debug/GNU-MacOSX/CDrawSequence.o CDrawSequence.cpp
CFASTA.h: In member function 'void CFASTA<SelectableItemType>::findLassoItemsInArea(juce::Array<Type, juce::DummyCriticalSection>&, int, int, int, int)':
CFASTA.h:258: error: invalid use of undefined type 'struct CDrawSequence'
CDrawSequence_fwd.h:4: error: forward declaration of 'struct CDrawSequence'
make[2]: *** [build/Debug/GNU-MacOSX/CDrawSequence.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 21s)


When I remove the "forward declaration" :

1
2
3
4
5
6
7
//in CDrawSequence_fwd.h
#ifndef _CDRAWSEQUENCE_FWD_H
#define	_CDRAWSEQUENCE_FWD_H
#include<map>
//class CDrawSequence;
typedef multimap<CHeaderAndSequence*,CDrawSequence*>::iterator mulimapIterator_TYPE;
#endif	/* _CDRAWSEQUENCE_FWD_H */ 


leads to the following errors:


g++-4.0    -c -g -w -MMD -MP -MF build/Debug/GNU-MacOSX/CDrawSequence.o.d -o build/Debug/GNU-MacOSX/CDrawSequence.o CDrawSequence.cpp
CDrawSequence_fwd.h:5: error: 'CDrawSequence' was not declared in this scope
CDrawSequence_fwd.h:5: error: template argument 2 is invalid
CDrawSequence_fwd.h:5: error: template argument 4 is invalid
CDrawSequence_fwd.h:5: error: expected initializer before 'mulimapIterator_TYPE'
CFASTA.h: In member function 'void CFASTA<SelectableItemType>::findLassoItemsInArea(juce::Array<Type, juce::DummyCriticalSection>&, int, int, int, int)':
CFASTA.h:256: error: 'mulimapIterator_TYPE' was not declared in this scope
CFASTA.h:256: error: expected `;' before 'i'
CFASTA.h:256: error: 'i' was not declared in this scope
make[2]: *** [build/Debug/GNU-MacOSX/CDrawSequence.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 22s)


Just to be absolutely thorough:

1
2
3
4
5
6
7
8
//in CDrawSequence_fwd.h
#ifndef _CDRAWSEQUENCE_FWD_H
#define	_CDRAWSEQUENCE_FWD_H
#include<map>
//class CDrawSequence;
#include"CDrawSequence.h"
typedef multimap<CHeaderAndSequence*,CDrawSequence*>::iterator mulimapIterator_TYPE;
#endif	/* _CDRAWSEQUENCE_FWD_H */ 


leads to the same errors:


g++-4.0    -c -g -w -MMD -MP -MF build/Debug/GNU-MacOSX/CDrawSequence.o.d -o build/Debug/GNU-MacOSX/CDrawSequence.o CDrawSequence.cpp
CDrawSequence_fwd.h:7: error: 'CDrawSequence' was not declared in this scope
CDrawSequence_fwd.h:7: error: template argument 2 is invalid
CDrawSequence_fwd.h:7: error: template argument 4 is invalid
CDrawSequence_fwd.h:7: error: expected initializer before 'mulimapIterator_TYPE'
CFASTA.h: In member function 'void CFASTA<SelectableItemType>::findLassoItemsInArea(juce::Array<Type, juce::DummyCriticalSection>&, int, int, int, int)':
CFASTA.h:256: error: 'mulimapIterator_TYPE' was not declared in this scope
CFASTA.h:256: error: expected `;' before 'i'
CFASTA.h:256: error: 'i' was not declared in this scope
make[2]: *** [build/Debug/GNU-MacOSX/CDrawSequence.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 22s)


Does anyone have any suggestions for me?


Last edited on Nov 24, 2009 at 10:38pm
Nov 25, 2009 at 2:45am
This is what your header should look like:

1
2
3
4
5
6
7
#ifndef _CDRAWSEQUENCE_FWD_H
#define	_CDRAWSEQUENCE_FWD_H
#include<map>
class CDrawSequence;
class CHeaderAndSequence;
typedef std::multimap<CHeaderAndSequence*,CDrawSequence*>::iterator mulimapIterator_TYPE;
#endif	/* _CDRAWSEQUENCE_FWD_H */  


There is nothing wrong with this code, and it should compile (it does here).

If you are still getting compiler errors, the problem may be related to another header. One you're including before this one.
Topic archived. No new replies allowed.