Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   overloading operator= (http://www.velocityreviews.com/forums/t277141-overloading-operator.html)

Daniel Allex 08-28-2003 03:13 AM

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=(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.


Josephine Schafer 08-28-2003 03:56 AM

Re: overloading operator=
 

"Daniel Allex" <dallex@erols.com> wrote in message
news:3F4D735D.214BFD54@erols.com...
> 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::operator=(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




All times are GMT. The time now is 06:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.