By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

class templates & constructor defn outside the class

P: n/a
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

Jun 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
gopal wrote:
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 )
{
...
}

--
Ian Collins.
Jun 17 '06 #2

P: n/a
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>

Ian Collins wrote:
gopal wrote:
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 )
{
..
}

--
Ian Collins.


Jun 17 '06 #3

P: n/a
On 17 Jun 2006 01:53:44 -0700, "gopal" <go*********@gmail.com> wrote:
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
Jun 17 '06 #4

P: n/a
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.

Roland Pibinger wrote:
On 17 Jun 2006 01:53:44 -0700, "gopal" <go*********@gmail.com> wrote:
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


Jun 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.