overloading operator=

D

Daniel Allex

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.
 
J

Josephine Schafer

Daniel Allex said:
I have the following code:

#include "iostream.h"
#include said:
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 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,086
Latest member
ChelseaAmi

Latest Threads

Top