|
| himanshu001 (4) | |
I am using visual studio VC6.0 as the compiler for below programme and getting a strange error written in the main function before the line causing the same Even moved the class and template memeber functions in a header file making them inline but the same error persists # include <iostream> using namespace std ; class templateArgument { public: void snapShot () { cout<<endl<<"I am snapShot::templateArgument () "<<endl; } }; // -------------Template Class-------------- // Purpose is to pass class templateArgument as <T> template<class T> class seedGenerator { public: seedGenerator (T objectGenerator) ; void snapShot () ; private: T m_objectGenerator ; protected: } ; template<class T> seedGenerator<T>::seedGenerator (T objectGenerator) { m_objectGenerator = objectGenerator; } template<class T> void seedGenerator<T>::snapShot () { m_objectGenerator.snapShot () ; } //------------------------------------------ //---- Class having template function -------------- // Non template class having template member function class seedGeneratorCreator { public: template<class T> seedGenerator<T> createSeed () ; }; template<class T> seedGenerator<T> seedGeneratorCreator::createSeed () { T object ; return seedGenerator<T> (object) ; } //--------------------------------------------------- void main () { seedGeneratorCreator factory ; // Error in the blow line as //error C2275: 'templateArgument2' : illegal use of this type as an expression factory.createSeed<templateArgument2> () ;// <---- Error causing line----- } | |
| guestgulkan (1303) | |
| What is templateArgument2 ? | |
| Hammurabi (399) | |
| Yeah. Just remove the 2. | |
| himanshu001 (4) | |
| My mistake templateArgument2 is templateArgument The original problem still persists even moving all template classes and definitions to header file and inlining them (VC6.0 environment) The actual code now is # include <iostream> using namespace std ; class templateArgument { public: void snapShot () { cout<<endl<<"I am snapShot::templateArgument () "<<endl; } }; // -------------Template Class-------------- // Purpose is to pass class templateArgument as <T> template<class T> class seedGenerator { public: seedGenerator (T objectGenerator) ; void snapShot () ; private: T m_objectGenerator ; protected: } ; template<class T> seedGenerator<T>::seedGenerator (T objectGenerator) { m_objectGenerator = objectGenerator; } template<class T> void seedGenerator<T>::snapShot () { m_objectGenerator.snapShot () ; } //------------------------------------------ //---- Class having template function -------------- // Non template class having template member function class seedGeneratorCreator { public: template<class T> seedGenerator<T> createSeed () ; }; template<class T> seedGenerator<T> seedGeneratorCreator::createSeed () { T object ; return seedGenerator<T> (object) ; } //--------------------------------------------------- void main () { seedGeneratorCreator factory ; // Error in the blow line as //error C2275: templateArgument ' : illegal use of this type as an expression factory.createSeed<templateArgument > () ;// <---- Error causing line----- } | |
| Hammurabi (399) | |
| It seems to work for me. You may need to update your compiler. | |
| guestgulkan (1303) | |||||
| It is a problem in VC 6.0 Yes - he needs to update his compiler as suggested by Hammurabi There is a solution if you are stuck with VC6.0 as follows: 1. Move the implemation of the seedGeneratorCreator::createSeed to inside the class, and make it take a parameter of type T. Like this:
Now call it like this
The best solution however is to upgrade your compiler - VC6.0 has shakey template support. | |||||
Last edited on | |||||
This topic is archived - New replies not allowed.
