473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

class templates & constructor defn outside the class

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
4 3055
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
12
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when...
4
by: asterixgallier | last post by:
Hello at all, i've got the following problem: - I define a class template in a header file - I implement the methods in a cpp file - I want to build an instance of my class template this...
11
by: dalu.gelu | last post by:
Hi, can anyone help me by writing a sample code of defining a copy constructor in a class having data member as an object of another class. for eg: class A{ int x; public: A(){ x=6;} };
8
by: Malciah | last post by:
I posted this on another site, but so far I've had no answers. So, I decided to try it here. -------------------------------------------------------- I've been learning C++ for about 6 weeks now,...
11
by: merrittr | last post by:
Hi in the code below in the main the hash table from K&R example replaces a node when a collision occurs What I am trying to do is set it up to "chain" another structure beside the node that it...
16
by: mdh | last post by:
Hi all, I have a question about an aspect of the hash table implementation from p 145. The code, stripped as far as possble, is as follows. /*******/ #define HASHSIZE 101
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.