class templates & constructor defn outside the class

G

gopal

I have the following class template declaration

//class template declaration
template <class T> class A {
public:
T t1;
A(T);
};

I would like to define the class A constrcutor outside. Any help
please?

Regards
JK
 
I

Ian Collins

gopal said:
I have the following class template declaration

//class template declaration
template <class T> class A {
public:
T t1;
A(T);
};

I would like to define the class A constrcutor outside. Any help
please?
Well that all depends on how your compiler looks for class template
member definitions. If it supports 'export' or uses other means to
locate them, you can put the definition in an appropriate source file.
Otherwise you have to include the definition in the header.

template <class T>
A<T>::A( T )
{
...
}
 
G

gopal

Hi tried the above code and i got the following error

1. __ctor' : is not a member of 'A<T>
2. A<T>' : template-class-id redefined as a global function
3. initializing' : cannot convert from 'const int' to 'class
A<int>
 
R

Roland Pibinger

Hi tried the above code and i got the following error

1. __ctor' : is not a member of 'A<T>
2. A<T>' : template-class-id redefined as a global function
3. initializing' : cannot convert from 'const int' to 'class
A<int>

The following should work:

template <class T> class A {
public:
T t1;
A(T);
};

template <class T>
A<T>::A ( T t): t1(t)
{
}

int main() {
A<int> test (3);
}

Best wishes,
Roland Pibinger
 
G

gopal

Hi, I am sorry, i did the following mistake -

I dint declare the constrcutor with argument inside the class body.
That is the reason for the error.

Thanks a lot.

JK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top