overloading operator=

Discussion in 'C++' started by Daniel Allex, Aug 28, 2003.

  1. Daniel Allex

    Daniel Allex Guest

    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::eek:perator=(int op)
    {

    return 1;
    }

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

    I get the following compiler errors:

    C:\Code\test\test.cpp(16) : error C2143: syntax error : missing ';'
    before 'tag::id'
    C:\Code\test\test.cpp(16) : error C2501: 'my_struct' : missing
    storage-class or type specifiers
    C:\Code\test\test.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.
    Daniel Allex, Aug 28, 2003
    #1
    1. Advertising

  2. "Daniel Allex" <> wrote in message
    news:...
    > I have the following code:
    >
    > #include "iostream.h"

    #include <iostream>

    >
    > class a
    > {
    > public:
    > struct my_struct
    > {
    > int one;
    > int two;
    > };
    >
    > my_struct operator= (int);


    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);

    > };
    >
    >
    > my_struct a::eek:perator=(int op)

    Same comment as above.
    > {
    >
    > return 1;


    return *this;
    > }
    >


    > main()


    main always returns int.
    int main ()
    > {
    > cout<<"Test"<<endl;


    Everything in standard headers is now in std namespace.
    So above line should be -
    std::cout << "Test"<<std::endl;

    > return 0;
    > }
    >


    HTH.

    --
    J.Schafer
    Josephine Schafer, Aug 28, 2003
    #2
    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. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    727
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    724
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    557
  5. Replies:
    2
    Views:
    306
Loading...

Share This Page