Jump to content
  • Advertisement
Sign in to follow this  
flounder

operator overloading problem

This topic is 4474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

the following code gives me the error: "expected constructor, destructor, or type conversion before ';' token", for both of the operator= lines. I'm trying to make it so the class my_int can be set equal to an int and an int can be set equal to my_int (i know i don't need a my_int class if it just holds an int, i made it to show the problem).
//my int.h
#ifndef myint_h
#define myint_h

class my_int
{
     public:
     int val;
     my_int(void);
     ~my_int(void);
};

operator=(const my_int& a,const int x);
operator=(const int& x,const my_int a);

#endif

//my int.cpp
#include "my int.h"

my_int::my_int(void)
{
     val=0;
}

my_int::~my_int(void) {}

operator=(const my_int& a,const int x)
{
     a.val=x;
}

operator=(const int& x,const my_int a)
{
     x=a.val;
}

does anyone know why the compiler is giving me this error or how to fix it?

Share this post


Link to post
Share on other sites
Advertisement
operator= has to be a member. Also, it has to have a return type. The cannonical declaration is


class foo
{
public:
foo& operator=(const bar& rhs)
{
//do stuff
return *this;
}
};

Share this post


Link to post
Share on other sites
If you want to convert data from a class to a primitive data type you can define a conversion operator for the primitive datatype and still having it as a member function of the class. The following code demonstrates that.

#include <iostream>

class my_int
{
public:
int val;

//assignment operator from int to my_int
my_int operator = (int x)
{
val = x;
return *this; //return copy of self
}

//conversion operator from my_int to int
operator int()
{
return val;
}
};

int main()
{
my_int mi;
int i;

mi = 1; //call assignment operator
i = mi; //call conversion operator

std::cout << "mi: " << mi.val << " i: " << i << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites
thanks to you both, Dim's is the one i'm looking for since it can go from it has class->primitive, primitive->class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
operator= has to be a member. Also, it has to have a return type. The cannonical declaration is


class foo
{
public:
foo& operator=(const bar& rhs)
{
//do stuff
return *this;
}
};


Yep... the assignment operator should be a member.

(as an aside)


foo &operator [smile](const foo &rhs) const;


Certain
std::
templates require the trailing
const
to indicate the original class object is const.

==
!=
>
<
>=
<=

etc

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
You can't assign to a const object. And operator smiley?


You were right, of course.

I guess "(as an aside)" didn't convey my message clearly enough.
[smiley] was just a substitute for all the operators listed.

Share this post


Link to post
Share on other sites
Those operators don't need to be members. Many feel that they shouldn't be members. And in such a case, they just take both arguments as const anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!