Template member function of NON template class


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-----
}
What is templateArgument2 ?
Yeah. Just remove the 2.
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-----
}
It seems to work for me. You may need to update your compiler.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//---- Class having template function --------------
// Non template class having template member function
class seedGeneratorCreator 
{
public:


//implementation now inside class and takes a parameter of type T
    template<class T>
 seedGenerator<T> createSeed (T t) 
{
    ///T object ; no longer required
    return seedGenerator<T> (t) ;
}

};



Now call it like this
1
2
3
4
5
6
7
8
9
10
int main()
{
	
   seedGeneratorCreator factory ;

    //should no longer give error C2275
   factory.createSeed( templateArgument() ); //call with object of required type
    
 return 0; 
}


The best solution however is to upgrade your compiler - VC6.0 has shakey template support.
Last edited on
Topic archived. No new replies allowed.