operator overloading problem

Started by
6 comments, last by Deyja 17 years, 11 months ago
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?
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;   }};
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;	}
thanks to you both, Dim's is the one i'm looking for since it can go from it has class->primitive, primitive->class.
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
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
You can't assign to a const object. And operator smiley?
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.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.

This topic is closed to new replies.

Advertisement