Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading operator=

Reply
Thread Tools

overloading operator=

 
 
Daniel Allex
Guest
Posts: n/a
 
      08-28-2003
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: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.

 
Reply With Quote
 
 
 
 
Josephine Schafer
Guest
Posts: n/a
 
      08-28-2003

"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: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57