Templates, copy ctor and type-conversion ctor

Discussion in 'C++' started by NVH, Jun 25, 2006.

  1. NVH

    NVH Guest

    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.
    NVH, Jun 25, 2006
    #1
    1. Advertising

  2. NVH

    mlimber Guest

    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
    mlimber, Jun 25, 2006
    #2
    1. Advertising

  3. NVH

    Jim Langston Guest

    "NVH" <> wrote in message
    news:...
    > 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.
    Jim Langston, Jun 25, 2006
    #3
  4. NVH

    NVH Guest

    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.
    NVH, Jun 28, 2006
    #4
  5. NVH

    Guest

    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, 2006
    #5
  6. NVH

    mlimber Guest

    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-lite/how-to-post.html#faq-5.9

    Cheers! --M
    mlimber, Jun 28, 2006
    #6
  7. NVH

    NVH Guest

    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.
    NVH, Jul 6, 2006
    #7
  8. NVH

    NVH Guest

    :) 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.
    NVH, Jul 6, 2006
    #8
  9. NVH

    mlimber Guest

    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
    mlimber, Jul 6, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    523
    velthuijsen
    Apr 16, 2004
  2. Grizlyk
    Replies:
    8
    Views:
    480
    Grizlyk
    Nov 29, 2006
  3. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    405
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  4. puzzlecracker
    Replies:
    8
    Views:
    425
    James Kanze
    Apr 15, 2008
  5. , India
    Replies:
    2
    Views:
    456
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page