compilation error with ctor

Discussion in 'C++' started by subramanian, Jan 6, 2007.

  1. subramanian

    subramanian Guest

    Consider the following program:

    #include <iostream>
    #include <string>

    class Member {
    int x;
    int y;
    public:
    Member(int argx, int argy);
    Member(const Member &m);
    void operator=(const Member &m);
    };

    Member::Member(int argx, int argy)
    {
    x = argx;
    y = argy;
    return;
    }

    Member::Member(const Member &m)
    {
    x = m.x;
    y = m.y;
    std::cout << "from copy ctor of Member class : x = " << x << " y = "
    << y << '\n';
    return;
    }

    void Member::eek:perator=(const Member &m)
    {
    x = m.x;
    y = m.y;
    std::cout << "from operator= : x = " << x << " y = " << y << '\n';
    return;
    }

    class Test {
    public:
    Test(const Member &argm1, const Member &argm2, const std::string
    &val);
    private:
    std::string str;
    Member member1;
    Member member2;
    };

    Test::Test(const Member &argm1, const Member &argm2, const std::string
    &val)
    {
    str = val;
    member1 = argm1;
    member2 = argm2;
    std::cout << "from one arg ctor : " << str << '\n';
    return;
    }

    int main(void)
    {
    Member argm1(10, 20);
    Member argm2(100, 200);

    Test tmp(argm1, argm2, "tmp object");

    return 0;
    }

    If I compile this program with VC++ 2005 Express edition, it gives
    compilation error at the line

    Test::Test(const Member &argm1, const Member &argm2, const std::string
    &val)

    saying

    'Member' : no appropriate default constructor available

    Similar error occurs with g++ also.

    If I change this ctor to

    Test::Test(const Member &argm1, const Member &argm2, const std::string
    &val) : member1(argm1), member2(arg2)
    {
    str = val;
    std::cout << "from one arg ctor : " << str << '\n';
    return;
    }

    then the compilation error goes. Though str is of class type
    std::string, str = val is accepted. However the same does not hold for
    class type Member for member1 = argm1 and member2 = argm2.
    What is the difference ?
    subramanian, Jan 6, 2007
    #1
    1. Advertising

  2. subramanian

    Ondra Holub Guest

    subramanian napsal:
    > Consider the following program:
    >
    > #include <iostream>
    > #include <string>
    >
    > class Member {
    > int x;
    > int y;
    > public:
    > Member(int argx, int argy);
    > Member(const Member &m);
    > void operator=(const Member &m);
    > };
    >
    > Member::Member(int argx, int argy)
    > {
    > x = argx;
    > y = argy;
    > return;
    > }
    >
    > Member::Member(const Member &m)
    > {
    > x = m.x;
    > y = m.y;
    > std::cout << "from copy ctor of Member class : x = " << x << " y = "
    > << y << '\n';
    > return;
    > }
    >
    > void Member::eek:perator=(const Member &m)
    > {
    > x = m.x;
    > y = m.y;
    > std::cout << "from operator= : x = " << x << " y = " << y << '\n';
    > return;
    > }
    >
    > class Test {
    > public:
    > Test(const Member &argm1, const Member &argm2, const std::string
    > &val);
    > private:
    > std::string str;
    > Member member1;
    > Member member2;
    > };
    >
    > Test::Test(const Member &argm1, const Member &argm2, const std::string
    > &val)
    > {
    > str = val;
    > member1 = argm1;
    > member2 = argm2;
    > std::cout << "from one arg ctor : " << str << '\n';
    > return;
    > }
    >
    > int main(void)
    > {
    > Member argm1(10, 20);
    > Member argm2(100, 200);
    >
    > Test tmp(argm1, argm2, "tmp object");
    >
    > return 0;
    > }
    >
    > If I compile this program with VC++ 2005 Express edition, it gives
    > compilation error at the line
    >
    > Test::Test(const Member &argm1, const Member &argm2, const std::string
    > &val)
    >
    > saying
    >
    > 'Member' : no appropriate default constructor available
    >
    > Similar error occurs with g++ also.
    >
    > If I change this ctor to
    >
    > Test::Test(const Member &argm1, const Member &argm2, const std::string
    > &val) : member1(argm1), member2(arg2)
    > {
    > str = val;
    > std::cout << "from one arg ctor : " << str << '\n';
    > return;
    > }
    >
    > then the compilation error goes. Though str is of class type
    > std::string, str = val is accepted. However the same does not hold for
    > class type Member for member1 = argm1 and member2 = argm2.
    > What is the difference ?


    See
    http://groups.google.com/group/comp...dd6693/8d07a2c131576a24?#doc_7dcd9695f701fc1f
    It is explained there.

    In fact you need default constructor for
    class X
    {
    public:
    X()
    // Here is called default constructor of member
    {
    member = something;
    }

    private:
    Member member;
    };
    Ondra Holub, Jan 6, 2007
    #2
    1. Advertising

  3. subramanian

    Ron Natalie Guest

    subramanian wrote:

    >
    > Test::Test(const Member &argm1, const Member &argm2, const std::string
    > &val) : member1(argm1), member2(arg2)
    > {
    > str = val;
    > std::cout << "from one arg ctor : " << str << '\n';
    > return;
    > }
    >
    > then the compilation error goes. Though str is of class type
    > std::string, str = val is accepted. However the same does not hold for
    > class type Member for member1 = argm1 and member2 = argm2.
    > What is the difference ?
    >

    The issue has nothing to do with the assigment (str = val). The issue
    is that you don't provide any initializers to the members when they
    are constructed, so there needs to be a default constructor.

    std::string has a default constructor.
    Member doesn't.

    If you want the behavior you are looking for, provide a default
    construct to member. It could be as simple change as:

    Member(int argx = 0, int argy = 0);


    Other than the debug prints in the copy constuctor and copy
    assignment operators, they do nothing that the implicitly
    generated ones wouldn't have done (except for the unconventional
    operator= returning void). If this is all they will ever do,
    get rid of yours.
    Ron Natalie, Jan 6, 2007
    #3
    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:
    504
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    473
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    456
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    394
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. Anonymous
    Replies:
    2
    Views:
    366
    Victor Bazarov
    Aug 28, 2007
Loading...

Share This Page