473,808 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates, copy ctor and type-conversion ctor

NVH
I know that this question may have been asked before but I can't
find it here so:
If there is a class:
class Foo
{
...
Foo (const Foo &num); // Copy constructor

template <typename T>
Foo (const T &num); // Conversion constructor
...
}

Then what's wrong with it and how can I fix it so that it may act
like this?

Thank you for any response to this.

Jun 24 '06 #1
8 2882
NVH wrote:
I know that this question may have been asked before but I can't
find it here so:
If there is a class:
class Foo
{
...
Foo (const Foo &num); // Copy constructor

template <typename T>
Foo (const T &num); // Conversion constructor
...
}

Then what's wrong with it and how can I fix it so that it may act
like this?
What exactly are you trying (but failing) to do?
Thank you for any response to this.


Oh, you're welcome.

Cheers! --M

Jun 25 '06 #2
"NVH" <da***********@ gmail.com> wrote in message
news:11******** ************@r2 g2000cwb.google groups.com...
I know that this question may have been asked before but I can't
find it here so:
If there is a class:
class Foo
{
...
Foo (const Foo &num); // Copy constructor

template <typename T>
Foo (const T &num); // Conversion constructor
...
}

Then what's wrong with it and how can I fix it so that it may act
like this?
I don't know, what's wrong with it? What's the problem? Does it not
compile? Does it not work the way you expect it?

And act like what? You gotta tell us what the problem is.
Thank you for any response to this.

Jun 25 '06 #3
NVH
Yeah, sorry about that.
Here's a more clear one:

=============== =============== =====
#include <iostream>
using namespace std;

class Foo
{
public:

Foo (const Foo &num)
{
number = num.number;
cout << 1 << endl;
} // Copy constructor

template <typename T>
Foo (const T &num)
{
number = num;
cout << 2 << endl;
} // Conversion constructor

void print (void)
{
cout << number << endl;
}
private:
auto int number; // Complier points to here for 1st
error
};

void main (void)
{
auto Foo G(5);
auto Foo H(G); // Compiler points to here for 2nd error

G.print(); // Display the content
H.print(); // Display the content
}
=============== =============== ==========

The compiler keeps on saying:
error C2071: 'number' : illegal storage class
error C2668: 'Foo::Foo' : ambiguous call to overloaded
function

Agian, thanks for any help is apreciated.

Jun 28 '06 #4

NVH wrote:
Yeah, sorry about that.
Here's a more clear one:

=============== =============== =====
#include <iostream>
using namespace std;

class Foo
{
public:

Foo (const Foo &num)
{
number = num.number;
cout << 1 << endl;
} // Copy constructor

template <typename T>
Foo (const T &num)
{
number = num;
cout << 2 << endl;
} // Conversion constructor

void print (void)
{
cout << number << endl;
}
private:
auto int number; // Complier points to here for 1st
error
};

void main (void)
{
auto Foo G(5);
auto Foo H(G); // Compiler points to here for 2nd error

G.print(); // Display the content
H.print(); // Display the content
}
=============== =============== ==========

The compiler keeps on saying:
error C2071: 'number' : illegal storage class
error C2668: 'Foo::Foo' : ambiguous call to overloaded
function

Agian, thanks for any help is apreciated.


first error: you can't use auto here, in a struct/class - only in a
function, but you shouldn't even bother using it there either.
2nd: I suspect it is confused between the 2 constructors - either would
work. Although I would think it should choose the non-templated
version. What compiler are you using?

-Tony

Jun 28 '06 #5
NVH wrote:
Yeah, sorry about that.
Here's a more clear one:

=============== =============== =====
#include <iostream>
using namespace std;

class Foo
{
public:

Foo (const Foo &num)
{
number = num.number;
cout << 1 << endl;
} // Copy constructor

template <typename T>
Foo (const T &num)
{
number = num;
cout << 2 << endl;
} // Conversion constructor

void print (void)
{
cout << number << endl;
}
private:
auto int number; // Complier points to here for 1st
error
Drop the auto. It's illegal here since you're not actually allocating
anything but rather just declaring a class that will be allocated
(either automatically or dynamically) later.
};

void main (void)
{
auto Foo G(5);
auto Foo H(G); // Compiler points to here for 2nd error
Again, drop the auto. It's legal here, but it's the default, and no one
uses it since it only adds clutter and verbosity.
G.print(); // Display the content
H.print(); // Display the content
}
=============== =============== ==========

The compiler keeps on saying:
error C2071: 'number' : illegal storage class
error C2668: 'Foo::Foo' : ambiguous call to overloaded
function


The problem is not with the code. It is legal, and various relatively
conformant compilers accept it fine (once the auto is deleted above,
that is). Thus, I will venture to guess that you are using VC++ 6,
which is non-conformant when it comes to templates. If that is the
case, you'll want to upgrade or search for a work-around that is
suitable for your situation. One might be to get rid of the copy
constructor Foo::Foo(const Foo&). For others, you'll want to ask in
Microsoft newsgroup since this is a compiler-specific issue. Several
such groups can be found here:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Cheers! --M

Jun 28 '06 #6
NVH
Sorry it takes so long for me to respond. I'm using Microsoft
Visual C++. I got rid of all the "auto" in the code (even the "auto"
next to "int") but the 2nd error still exist.
Thanks for responding to my problem.

Jul 6 '06 #7
NVH
:) I just got rid of the copy constructor and it work so Thanks
a lot to both you and Tony. But quick question: If I wanted to have
the copy constructor to do something else rather then the template,
what would I have to do?
For example I'd like something like this:
class Foo
{
public:

Foo (const Foo &num)
{
number = num;
cout << 3 << endl;
} // Copy constructor

template <typename T>
Foo (const T &num)
{
number = num;
cout << 2 << endl;
} // Conversion constructor

void print (void)
{
cout << number << endl;
}
private:
int number;
};

void main (void)
{
Foo G(5);
Foo H(G);

G.print(); // Display the content
H.print(); // Display the content
}

I'd like "2" to be printed out if the inputted type ("T") is not of
type Foo and "3" to be printed out if the type ("T") is of type Foo.

Thanks agian for your help.

Jul 6 '06 #8
NVH wrote:
:) I just got rid of the copy constructor and it work so Thanks
a lot to both you and Tony. But quick question: If I wanted to have
the copy constructor to do something else rather then the template,
what would I have to do?
For example I'd like something like this:
class Foo
{
public:

Foo (const Foo &num)
{
number = num;
cout << 3 << endl;
} // Copy constructor

template <typename T>
Foo (const T &num)
{
number = num;
cout << 2 << endl;
} // Conversion constructor

void print (void)
{
cout << number << endl;
}
private:
int number;
};

void main (void)
{
Foo G(5);
Foo H(G);

G.print(); // Display the content
H.print(); // Display the content
}

I'd like "2" to be printed out if the inputted type ("T") is not of
type Foo and "3" to be printed out if the type ("T") is of type Foo.

Thanks agian for your help.
The easiest way is to upgrade to a better compiler that won't barf on
valid code. You can download a free one from Microsoft.

Cheers! --M

Jul 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7178
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
2
2177
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
1
3978
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
3
1929
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm still feeling confused. Suppose I have a class template: template <typename T> class Foo {
7
567
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
25
3339
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the same subject in clc++m to get the more of the context. Ted
8
3105
by: sagi.perel | last post by:
I have tried to compile the following code on Win & Unix. Doesn't work on either. <----- CODE -----> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <vector> #include <typeinfo> using namespace std;
3
2058
by: tschwartz | last post by:
I'm trying to write a stylesheet which removes nodes which are empty as a result of other template processing. For example, given the following xml, I'd like to: - remove all "b" elements - remove all "a" elements which, as a result of "b" element removal, now have no children so, starting with:
3
2793
by: abhishek.smu | last post by:
Given an XML like: <root> <node>8</node> <node>21</node> <node>-7</node> <node>13</node> <node>43</node> <node>2</node> </root>
9
1642
by: patrik.nyman | last post by:
I have this templates to mark up hyphenation over line breaks: <xsl:template match="reg"> <xsl:apply-templates select="@orig"/> </xsl:template> <xsl:template match="reg/@orig"> <xsl:call-template name="html-hyphens"/> </xsl:template>
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.