This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
newbie struggling with template classes.
- To: gcc-help at gcc dot gnu dot org
- Subject: newbie struggling with template classes.
- From: Barry Mathieu <barkatbarry at mindspring dot com>
- Date: Fri, 9 Nov 2001 00:47:45 -0500
Yes, I am a major newbie to C++ and gcc (not my profession; a hobby).
I'm trying to become competent in an endeavor to potentially become a
contributor to Open Source Software.
I have successfully compiled and run a program that declares a class
defined variable both in main() and in a function. As I am in the
process of learning class templates, I attempted to alter this program
to utilize the class template method.
I think there is a problem instantiating the same class template in
main() and again in a function called by main.
I did some reading, and I believe `-fno-external-templates' is somehow
used to handle this issue.
In the client code, I have placed before main()
#pragma implementation "student.h"
#include "student.h" //custom name ADT
typedef StudentRecord<ifstream> Test;
I declare the template class in main() with:
Test firstRecord, secondRecord;
and then in the function:
Test leftRecord, rightRecord;
I have combined the class specification and implementation file into one
file, "student.h"
In the "student.h" file, I have placed amongst the preprocessor
directives,
#pragma interface
Following the preprocessor section:
template<class ItemType>
Member function headings appear as:
template<class ItemType>
void StudentRecord<ItemType>::Input( ItemType& file )
Only one member function receives the class template argument. Other
member functions either don't have parameters, or are overloaded
operators.
I then issue the following command to compile & link:
g++ project2.cpp -fno-implicit-templates -o proj2
Am I just not understanding template class instantiation? If you know of
a decent on-line reference, or book, that could help to understand these
concepts, I would appreciate your suggestion.
Barry