overloading operator=

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Allex

    overloading operator=

    I have the following code:

    #include "iostream.h "

    class a
    {
    public:
    struct my_struct
    {
    int one;
    int two;
    };

    my_struct operator= (int);
    };


    my_struct a::operator=(in t op)
    {

    return 1;
    }

    main()
    {
    cout<<"Test"<<e ndl;
    return 0;
    }

    I get the following compiler errors:

    C:\Code\test\te st.cpp(16) : error C2143: syntax error : missing ';'
    before 'tag::id'
    C:\Code\test\te st.cpp(16) : error C2501: 'my_struct' : missing
    storage-class or type specifiers
    C:\Code\test\te st.cpp(16) : fatal error C1004: unexpected end of file
    found

    If I change the return type to and int or some other defined type for
    the overloaded operator, it compiles fine.

  • Josephine Schafer

    #2
    Re: overloading operator=


    "Daniel Allex" <dallex@erols.c om> wrote in message
    news:3F4D735D.2 14BFD54@erols.c om...[color=blue]
    > I have the following code:
    >
    > #include "iostream.h "[/color]
    #include <iostream>
    [color=blue]
    >
    > class a
    > {
    > public:
    > struct my_struct
    > {
    > int one;
    > int two;
    > };
    >
    > my_struct operator= (int);[/color]

    The return type of assignment operator is the left hand side operand (object
    on which the
    function is invoked). So my_struct as return type is incorrect. Also one
    should return by reference.
    So it's return type should be a& (reference to a).
    a& operator= (int);
    [color=blue]
    > };
    >
    >
    > my_struct a::operator=(in t op)[/color]
    Same comment as above.[color=blue]
    > {
    >
    > return 1;[/color]

    return *this;[color=blue]
    > }
    >[/color]
    [color=blue]
    > main()[/color]

    main always returns int.
    int main ()[color=blue]
    > {
    > cout<<"Test"<<e ndl;[/color]

    Everything in standard headers is now in std namespace.
    So above line should be -
    std::cout << "Test"<<std::en dl;
    [color=blue]
    > return 0;
    > }
    >[/color]

    HTH.

    --
    J.Schafer


    Comment

    Working...