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

template probleme

P: n/a
Hi,

The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)
template <class T> class AutoPtr
{
public:
/** Default constructor. */
AutoPtr(T* p = 0);

/** Copy contructing an AutoPtr from another AutoPtr causes the old
one
* to be released from destruction duty. */
AutoPtr(const AutoPtr &a);

/** Copy contructing an AutoPtr from a pointer with a different element
type. */
template<class T1> AutoPtr(const AutoPtr<T1> &a);
}

devstudio says: member function already defined or declared
on the last constructor

thanks

yomgui
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2005-03-28, yomgui <no*@valid.com> wrote:
Hi,

The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)
template <class T> class AutoPtr
{
public:
/** Default constructor. */
AutoPtr(T* p = 0);

/** Copy contructing an AutoPtr from another AutoPtr causes the old
one
* to be released from destruction duty. */
AutoPtr(const AutoPtr &a);

/** Copy contructing an AutoPtr from a pointer with a different element
type. */
template<class T1> AutoPtr(const AutoPtr<T1> &a);
}

devstudio says: member function already defined or declared
on the last constructor


Looks like an issue with the compiler -- the parameterized constructor is
*not* a copy constructor.

Here's a question for you -- what is the output of this code on your compiler ?
The template constructor should not be called on copy -- the compiler should
still create one.

/*
template construct ...
conversion construct called!
calling copy construct ...
*/

#include <iostream>

class X {
public:

template <typename T> X(const T& ) {
std::cout << "conversion construct called!" << std::endl;
}
};
int main()
{
std::cout << "template construct ... " << std::endl;
X x(3);
std::cout << "calling copy construct ... " << std::endl;
X y(x);
}
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2

P: n/a
yomgui wrote:
The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)
You will have to upgrade your compiler to 7.1 or 8.0 Beta in order to
make it work. VC++ v6 is notoriously bad with member templates.
[...]

Jul 23 '05 #3

P: n/a
I've tried your test,
this is also my result:
template construct ...
conversion construct called!
calling copy construct ...


yomgui

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.